with_structured_output() — langchain Function Reference
Architecture documentation for the with_structured_output() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD f144b7f2_8059_57ad_4c98_4b3d77c48244["with_structured_output()"] 2a683305_667b_3567_cab9_9f77e29d4afa["BaseChatOpenAI"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|defined in| 2a683305_667b_3567_cab9_9f77e29d4afa a65d1edd_fa36_3eb2_3d8b_f23a51f56960["with_structured_output()"] a65d1edd_fa36_3eb2_3d8b_f23a51f56960 -->|calls| f144b7f2_8059_57ad_4c98_4b3d77c48244 11e3abdc_436d_b307_3a6a_78c6f1428ad0["_filter_disabled_params()"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|calls| 11e3abdc_436d_b307_3a6a_78c6f1428ad0 8e1c6ba4_0c19_ddac_8d04_68d2a775e1c4["bind_tools()"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|calls| 8e1c6ba4_0c19_ddac_8d04_68d2a775e1c4 a65d1edd_fa36_3eb2_3d8b_f23a51f56960["with_structured_output()"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|calls| a65d1edd_fa36_3eb2_3d8b_f23a51f56960 cdaeec71_b581_c482_d50c_4334d429196c["_is_pydantic_class()"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|calls| cdaeec71_b581_c482_d50c_4334d429196c 1b8cb178_42d5_1a67_7602_8228794247a8["_convert_to_openai_response_format()"] f144b7f2_8059_57ad_4c98_4b3d77c48244 -->|calls| 1b8cb178_42d5_1a67_7602_8228794247a8 style f144b7f2_8059_57ad_4c98_4b3d77c48244 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/chat_models/base.py lines 1961–2223
def with_structured_output(
self,
schema: _DictOrPydanticClass | None = None,
*,
method: Literal[
"function_calling", "json_mode", "json_schema"
] = "function_calling",
include_raw: bool = False,
strict: bool | None = None,
tools: list | None = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, _DictOrPydantic]:
"""Model wrapper that returns outputs formatted to match the given schema.
Args:
schema: The output schema. Can be passed in as:
- An OpenAI function/tool schema,
- A JSON Schema,
- A `TypedDict` class,
- Or a Pydantic class.
If `schema` is a Pydantic class then the model output will be a
Pydantic instance of that class, and the model-generated fields will be
validated by the Pydantic class. Otherwise the model output will be a
dict and will not be validated.
See `langchain_core.utils.function_calling.convert_to_openai_tool` for
more on how to properly specify types and descriptions of schema fields
when specifying a Pydantic or `TypedDict` class.
method: The method for steering model generation, one of:
- `'function_calling'`:
Uses OpenAI's [tool-calling API](https://platform.openai.com/docs/guides/function-calling)
(formerly called function calling)
- `'json_schema'`:
Uses OpenAI's [Structured Output API](https://platform.openai.com/docs/guides/structured-outputs)
- `'json_mode'`:
Uses OpenAI's [JSON mode](https://platform.openai.com/docs/guides/structured-outputs/json-mode).
Note that if using JSON mode then you must include instructions for
formatting the output into the desired schema into the model call
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:
- `True`:
Model output is guaranteed to exactly match the schema.
The input schema will also be validated according to the
[supported schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas?api-mode=responses#supported-schemas).
- `False`:
Input schema will not be validated and model output will not be
validated.
- `None`:
`strict` argument will not be passed to the model.
tools:
A list of tool-like objects to bind to the chat model. Requires that:
- `method` is `'json_schema'` (default).
- `strict=True`
- `include_raw=True`
If a model elects to call a tool, the resulting `AIMessage` in `'raw'`
will include tool calls.
??? example
```python
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does with_structured_output() do?
with_structured_output() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 1961.
What does with_structured_output() call?
with_structured_output() calls 5 function(s): _convert_to_openai_response_format, _filter_disabled_params, _is_pydantic_class, bind_tools, with_structured_output.
What calls with_structured_output()?
with_structured_output() is called by 1 function(s): with_structured_output.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free