Home / File/ structured_output.py — langchain Source File

structured_output.py — langchain Source File

Architecture documentation for structured_output.py, a python file in the langchain codebase. 10 imports, 0 dependents.

File python CoreAbstractions MessageSchema 10 imports 2 functions 9 classes

Entity Profile

Dependency Diagram

graph LR
  5e19de53_9b4f_8f39_3360_3d6743d67e80["structured_output.py"]
  7025b240_fdc3_cf68_b72f_f41dac94566b["json"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 7025b240_fdc3_cf68_b72f_f41dac94566b
  8dfa0cac_d802_3ccd_f710_43a5e70da3a5["uuid"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 8dfa0cac_d802_3ccd_f710_43a5e70da3a5
  aac5f8ad_7f2a_3a8e_3b4b_b07d681cbdcf["dataclasses"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> aac5f8ad_7f2a_3a8e_3b4b_b07d681cbdcf
  05bcd40b_a309_8a05_c2c6_59e32e113cf5["types"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 05bcd40b_a309_8a05_c2c6_59e32e113cf5
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 43d88577_548b_2248_b01b_7987bae85dcc
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> 91721f45_4909_e489_8c1f_084f8bd87145
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  5e19de53_9b4f_8f39_3360_3d6743d67e80 --> d758344f_537f_649e_f467_b9d7442e86df
  style 5e19de53_9b4f_8f39_3360_3d6743d67e80 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Types for setting agent response formats."""

from __future__ import annotations

import json
import uuid
from dataclasses import dataclass, is_dataclass
from types import UnionType
from typing import (
    TYPE_CHECKING,
    Any,
    Generic,
    Literal,
    TypeVar,
    Union,
    get_args,
    get_origin,
)

from langchain_core.tools import BaseTool, StructuredTool
from pydantic import BaseModel, TypeAdapter
from typing_extensions import Self, is_typeddict

if TYPE_CHECKING:
    from collections.abc import Callable, Iterable

    from langchain_core.messages import AIMessage

# Supported schema types: Pydantic models, dataclasses, TypedDict, JSON schema dicts
SchemaT = TypeVar("SchemaT")

SchemaKind = Literal["pydantic", "dataclass", "typeddict", "json_schema"]


class StructuredOutputError(Exception):
    """Base class for structured output errors."""

    ai_message: AIMessage


class MultipleStructuredOutputsError(StructuredOutputError):
    """Raised when model returns multiple structured output tool calls when only one is expected."""

    def __init__(self, tool_names: list[str], ai_message: AIMessage) -> None:
        """Initialize `MultipleStructuredOutputsError`.

        Args:
            tool_names: The names of the tools called for structured output.
            ai_message: The AI message that contained the invalid multiple tool calls.
        """
        self.tool_names = tool_names
        self.ai_message = ai_message

        super().__init__(
            "Model incorrectly returned multiple structured responses "
            f"({', '.join(tool_names)}) when only one is expected."
        )


class StructuredOutputValidationError(StructuredOutputError):
// ... (403 more lines)

Subdomains

Dependencies

  • collections.abc
  • dataclasses
  • json
  • langchain_core.messages
  • langchain_core.tools
  • pydantic
  • types
  • typing
  • typing_extensions
  • uuid

Frequently Asked Questions

What does structured_output.py do?
structured_output.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in structured_output.py?
structured_output.py defines 2 function(s): _parse_with_schema, collections.
What does structured_output.py depend on?
structured_output.py imports 10 module(s): collections.abc, dataclasses, json, langchain_core.messages, langchain_core.tools, pydantic, types, typing, and 2 more.
Where is structured_output.py in the architecture?
structured_output.py is located at libs/langchain_v1/langchain/agents/structured_output.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain_v1/langchain/agents).

Analyze Your Own Codebase

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

Try Supermodel Free