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
  e8d6a68e_9c70_dc39_2fed_312e948801f8["with_structured_output()"]
  19e4be00_71fb_5390_6768_f6e6158f49b4["ChatOllama"]
  e8d6a68e_9c70_dc39_2fed_312e948801f8 -->|defined in| 19e4be00_71fb_5390_6768_f6e6158f49b4
  c13fd55f_31c2_4cb3_a17b_a17f81e436ed["bind_tools()"]
  e8d6a68e_9c70_dc39_2fed_312e948801f8 -->|calls| c13fd55f_31c2_4cb3_a17b_a17f81e436ed
  833edee0_b6a7_157f_ee72_19c9e88c7dfc["_is_pydantic_class()"]
  e8d6a68e_9c70_dc39_2fed_312e948801f8 -->|calls| 833edee0_b6a7_157f_ee72_19c9e88c7dfc
  style e8d6a68e_9c70_dc39_2fed_312e948801f8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/ollama/langchain_ollama/chat_models.py lines 1256–1606

    def with_structured_output(
        self,
        schema: dict | type,
        *,
        method: Literal["function_calling", "json_mode", "json_schema"] = "json_schema",
        include_raw: bool = False,
        **kwargs: Any,
    ) -> Runnable[LanguageModelInput, dict | BaseModel]:
        r"""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:

                - `'json_schema'`:
                    Uses Ollama's [structured output API](https://ollama.com/blog/structured-outputs)
                - `'function_calling'`:
                    Uses Ollama's tool-calling API
                - `'json_mode'`:
                    Specifies `format='json'`. 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'`.

            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`

        !!! warning "Behavior changed in `langchain-ollama` 0.2.2"

            Added support for structured output API via `format` parameter.

        !!! warning "Behavior changed in `langchain-ollama` 0.3.0"

            Updated default `method` to `'json_schema'`.

        ??? note "Example: `schema=Pydantic` class, `method='json_schema'`, `include_raw=False`"

            ```python
            from typing import Optional

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/ollama/langchain_ollama/chat_models.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/ollama/langchain_ollama/chat_models.py at line 1256.
What does with_structured_output() call?
with_structured_output() calls 2 function(s): _is_pydantic_class, bind_tools.

Analyze Your Own Codebase

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

Try Supermodel Free