with_structured_output() — langchain Function Reference
Architecture documentation for the with_structured_output() function in chat_models.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD dc1fc791_1a25_e86b_f626_e8a7105a996c["with_structured_output()"] 36b59643_acfc_fb1d_752e_ae7ec32a79a4["ChatPerplexity"] dc1fc791_1a25_e86b_f626_e8a7105a996c -->|defined in| 36b59643_acfc_fb1d_752e_ae7ec32a79a4 a721f74e_a9bc_485c_1015_24805ba9e651["_is_pydantic_class()"] dc1fc791_1a25_e86b_f626_e8a7105a996c -->|calls| a721f74e_a9bc_485c_1015_24805ba9e651 style dc1fc791_1a25_e86b_f626_e8a7105a996c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/perplexity/langchain_perplexity/chat_models.py lines 716–816
def with_structured_output(
self,
schema: _DictOrPydanticClass | None = None,
*,
method: Literal["json_schema"] = "json_schema",
include_raw: bool = False,
strict: bool | None = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, _DictOrPydantic]:
"""Model wrapper that returns outputs formatted to match the given schema for Preplexity.
Currently, Perplexity only supports "json_schema" method for structured output
as per their [official documentation](https://docs.perplexity.ai/guides/structured-outputs).
Args:
schema: The output schema. Can be passed in as:
- a JSON Schema,
- a `TypedDict` class,
- or a Pydantic class
method: The method for steering model generation, currently only support:
- `'json_schema'`: Use the JSON Schema to parse the model output
include_raw:
If `False` then only the parsed structured output is returned.
If an error occurs during model output parsing it will be raised.
If `True` then both the raw model response (a `BaseMessage`) and the
parsed model response will be returned.
If an error occurs during output parsing it will be caught and returned
as well.
The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
`'parsing_error'`.
strict:
Unsupported: whether to enable strict schema adherence when generating
the output. This parameter is included for compatibility with other
chat models, but is currently ignored.
kwargs: Additional keyword args aren't supported.
Returns:
A `Runnable` that takes same inputs as a
`langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
`False` and `schema` is a Pydantic class, `Runnable` outputs an instance
of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
`False` then `Runnable` outputs a `dict`.
If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:
- `'raw'`: `BaseMessage`
- `'parsed'`: `None` if there was a parsing error, otherwise the type
depends on the `schema` as described above.
- `'parsing_error'`: `BaseException | None`
""" # noqa: E501
if method in ("function_calling", "json_mode"):
method = "json_schema"
if method == "json_schema":
if schema is None:
raise ValueError(
"schema must be specified when method is not 'json_schema'. "
"Received None."
)
is_pydantic_schema = _is_pydantic_class(schema)
response_format = convert_to_json_schema(schema)
llm = self.bind(
response_format={
"type": "json_schema",
"json_schema": {"schema": response_format},
},
ls_structured_output_format={
"kwargs": {"method": method},
"schema": response_format,
},
)
output_parser = (
ReasoningStructuredOutputParser(pydantic_object=schema) # type: ignore[arg-type]
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does with_structured_output() do?
with_structured_output() is a function in the langchain codebase, defined in libs/partners/perplexity/langchain_perplexity/chat_models.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/perplexity/langchain_perplexity/chat_models.py at line 716.
What does with_structured_output() call?
with_structured_output() calls 1 function(s): _is_pydantic_class.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free