Home / Function/ with_structured_output() — langchain Function Reference

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
  a484b53c_8c1c_5314_de44_ea0330c8aed2["with_structured_output()"]
  977b57b2_5d0e_bcf4_a43e_b52857105005["ChatAnthropic"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|defined in| 977b57b2_5d0e_bcf4_a43e_b52857105005
  57f22736_087b_6915_6548_5529978001fa["_get_llm_for_structured_output_when_thinking_is_enabled()"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|calls| 57f22736_087b_6915_6548_5529978001fa
  b4ebc2e5_c582_39ab_6403_117a559ab366["bind_tools()"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|calls| b4ebc2e5_c582_39ab_6403_117a559ab366
  b9533de7_77f0_ac15_f27f_92372b897314["convert_to_anthropic_tool()"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|calls| b9533de7_77f0_ac15_f27f_92372b897314
  5bd44152_3323_2e97_ed23_07de137480d2["_convert_to_anthropic_output_config_format()"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|calls| 5bd44152_3323_2e97_ed23_07de137480d2
  style a484b53c_8c1c_5314_de44_ea0330c8aed2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1548–1706

    def with_structured_output(
        self,
        schema: dict | type,
        *,
        include_raw: bool = False,
        method: Literal["function_calling", "json_schema"] = "function_calling",
        **kwargs: Any,
    ) -> Runnable[LanguageModelInput, dict | BaseModel]:
        """Model wrapper that returns outputs formatted to match the given schema.

        See the [LangChain docs](https://docs.langchain.com/oss/python/integrations/chat/anthropic#structured-output)
        for more details and examples.

        Args:
            schema: The output schema. Can be passed in as:

                - An Anthropic tool schema,
                - 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.
            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'`.
            method: The structured output method to use. Options are:

                - `'function_calling'` (default): Use forced tool calling to get
                    structured output.
                - `'json_schema'`: Use Claude's dedicated
                    [structured output](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
                    feature.

            kwargs: Additional keyword arguments are ignored.

        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`

        Example:
            ```python hl_lines="13"
            from langchain_anthropic import ChatAnthropic
            from pydantic import BaseModel, Field

            model = ChatAnthropic(model="claude-sonnet-4-5")

            class Movie(BaseModel):
                \"\"\"A movie with details.\"\"\"
                title: str = Field(..., description="The title of the movie")
                year: int = Field(..., description="The year the movie was released")
                director: str = Field(..., description="The director of the movie")
                rating: float = Field(..., description="The movie's rating out of 10")

Domain

Subdomains

Frequently Asked Questions

What does with_structured_output() do?
with_structured_output() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1548.
What does with_structured_output() call?
with_structured_output() calls 4 function(s): _convert_to_anthropic_output_config_format, _get_llm_for_structured_output_when_thinking_is_enabled, bind_tools, convert_to_anthropic_tool.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free