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
  fd162549_9cc7_f8f4_7877_2bc28c45cf20["with_structured_output()"]
  d5ca3c3a_3c29_0cb2_a156_35c92a31f5fd["ChatGroq"]
  fd162549_9cc7_f8f4_7877_2bc28c45cf20 -->|defined in| d5ca3c3a_3c29_0cb2_a156_35c92a31f5fd
  a2a1473b_d07e_f130_17fd_57f36ea7a804["bind_tools()"]
  fd162549_9cc7_f8f4_7877_2bc28c45cf20 -->|calls| a2a1473b_d07e_f130_17fd_57f36ea7a804
  f6a77a86_0ace_2251_5079_743b16f16151["_is_pydantic_class()"]
  fd162549_9cc7_f8f4_7877_2bc28c45cf20 -->|calls| f6a77a86_0ace_2251_5079_743b16f16151
  style fd162549_9cc7_f8f4_7877_2bc28c45cf20 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/groq/langchain_groq/chat_models.py lines 898–1260

    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]:
        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.

                !!! warning "Behavior changed in `langchain-groq` 0.3.8"

                    Added support for Groq's dedicated structured output feature via
                    `method="json_schema"`.

            method: The method for steering model generation, one of:

                - `'function_calling'`:
                    Uses Groq's tool-calling [API](https://console.groq.com/docs/tool-use)
                - `'json_schema'`:
                    Uses Groq's [Structured Output API](https://console.groq.com/docs/structured-outputs).
                    Supported for a subset of models, including `openai/gpt-oss`,
                    `moonshotai/kimi-k2-instruct-0905`, and some `meta-llama/llama-4`
                    models. See [docs](https://console.groq.com/docs/structured-outputs)
                    for details.
                - `'json_mode'`:
                    Uses Groq's [JSON mode](https://console.groq.com/docs/structured-outputs#json-object-mode).
                    Note that if using JSON mode then you must include instructions for
                    formatting the output into the desired schema into the model call

                Learn more about the differences between the methods and which models
                support which methods [here](https://console.groq.com/docs/structured-outputs).

            method:
                The method for steering model generation, either `'function_calling'`
                or `'json_mode'`. If `'function_calling'` then the schema will be converted
                to an OpenAI function and the returned model will make use of the
                function-calling API. If `'json_mode'` then JSON mode will be used.

                !!! note
                    If using `'json_mode'` then you must include instructions for formatting
                    the output into the desired schema into the model call. (either via the
                    prompt itself or in the system message/prompt/instructions).

                !!! warning
                    `'json_mode'` does not support streaming responses stop sequences.

            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:

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/groq/langchain_groq/chat_models.py.
Where is with_structured_output() defined?
with_structured_output() is defined in libs/partners/groq/langchain_groq/chat_models.py at line 898.
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