with_structured_output() — langchain Function Reference
Architecture documentation for the with_structured_output() function in huggingface.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 66ab56cc_d97c_b9a3_ace4_750c0873eaf8["with_structured_output()"] 8cf0d6c0_abf8_3ee2_fd00_8bfc8c02058a["ChatHuggingFace"] 66ab56cc_d97c_b9a3_ace4_750c0873eaf8 -->|defined in| 8cf0d6c0_abf8_3ee2_fd00_8bfc8c02058a e5b0db37_7a35_b8c7_c069_a78188b87396["bind_tools()"] 66ab56cc_d97c_b9a3_ace4_750c0873eaf8 -->|calls| e5b0db37_7a35_b8c7_c069_a78188b87396 style 66ab56cc_d97c_b9a3_ace4_750c0873eaf8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py lines 1090–1219
def with_structured_output(
self,
schema: dict | type[BaseModel] | None = None,
*,
method: Literal[
"function_calling", "json_mode", "json_schema"
] = "function_calling",
include_raw: bool = False,
**kwargs: Any,
) -> Runnable[LanguageModelInput, dict | BaseModel]:
"""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
Pydantic class is currently supported.
method: The method for steering model generation, one of:
- `'function_calling'`: uses tool-calling features.
- `'json_schema'`: uses dedicated structured output features.
- `'json_mode'`: uses JSON mode.
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'`.
kwargs:
Additional parameters to pass to the underlying LLM's
`langchain_core.language_models.chat.BaseChatModel.bind`
method, such as `response_format` or `ls_structured_output_format`.
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`
"""
_ = kwargs.pop("strict", None)
if kwargs:
msg = f"Received unsupported arguments {kwargs}"
raise ValueError(msg)
is_pydantic_schema = isinstance(schema, type) and is_basemodel_subclass(schema)
if method == "function_calling":
if schema is None:
msg = (
"schema must be specified when method is 'function_calling'. "
"Received None."
)
raise ValueError(msg)
formatted_tool = convert_to_openai_tool(schema)
tool_name = formatted_tool["function"]["name"]
llm = self.bind_tools(
[schema],
tool_choice=tool_name,
ls_structured_output_format={
"kwargs": {"method": "function_calling"},
"schema": formatted_tool,
},
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/huggingface/langchain_huggingface/chat_models/huggingface.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py at line 1090.
What does with_structured_output() call?
with_structured_output() calls 1 function(s): bind_tools.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free