Home / Class/ ToolStrategy Class — langchain Architecture

ToolStrategy Class — langchain Architecture

Architecture documentation for the ToolStrategy class in structured_output.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4ecead60_ac3b_7004_f7d6_8d475c41d81d["ToolStrategy"]
  c2936c24_74cd_6911_037b_0f67eebfefee["structured_output.py"]
  4ecead60_ac3b_7004_f7d6_8d475c41d81d -->|defined in| c2936c24_74cd_6911_037b_0f67eebfefee
  b0c9fec5_c27c_af1a_7146_cef231d28fa3["__init__()"]
  4ecead60_ac3b_7004_f7d6_8d475c41d81d -->|method| b0c9fec5_c27c_af1a_7146_cef231d28fa3

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/structured_output.py lines 195–257

class ToolStrategy(Generic[SchemaT]):
    """Use a tool calling strategy for model responses."""

    schema: type[SchemaT] | UnionType | dict[str, Any]
    """Schema for the tool calls."""

    schema_specs: list[_SchemaSpec[Any]]
    """Schema specs for the tool calls."""

    tool_message_content: str | None
    """The content of the tool message to be returned when the model calls
    an artificial structured output tool.
    """

    handle_errors: (
        bool | str | type[Exception] | tuple[type[Exception], ...] | Callable[[Exception], str]
    )
    """Error handling strategy for structured output via `ToolStrategy`.

    - `True`: Catch all errors with default error template
    - `str`: Catch all errors with this custom message
    - `type[Exception]`: Only catch this exception type with default message
    - `tuple[type[Exception], ...]`: Only catch these exception types with default
        message
    - `Callable[[Exception], str]`: Custom function that returns error message
    - `False`: No retry, let exceptions propagate
    """

    def __init__(
        self,
        schema: type[SchemaT] | UnionType | dict[str, Any],
        *,
        tool_message_content: str | None = None,
        handle_errors: bool
        | str
        | type[Exception]
        | tuple[type[Exception], ...]
        | Callable[[Exception], str] = True,
    ) -> None:
        """Initialize `ToolStrategy`.

        Initialize `ToolStrategy` with schemas, tool message content, and error handling
        strategy.
        """
        self.schema = schema
        self.tool_message_content = tool_message_content
        self.handle_errors = handle_errors

        def _iter_variants(schema: Any) -> Iterable[Any]:
            """Yield leaf variants from Union and JSON Schema oneOf."""
            if get_origin(schema) in {UnionType, Union}:
                for arg in get_args(schema):
                    yield from _iter_variants(arg)
                return

            if isinstance(schema, dict) and "oneOf" in schema:
                for sub in schema.get("oneOf", []):
                    yield from _iter_variants(sub)
                return

            yield schema

        self.schema_specs = [_SchemaSpec(s) for s in _iter_variants(schema)]

Frequently Asked Questions

What is the ToolStrategy class?
ToolStrategy is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/structured_output.py.
Where is ToolStrategy defined?
ToolStrategy is defined in libs/langchain_v1/langchain/agents/structured_output.py at line 195.

Analyze Your Own Codebase

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

Try Supermodel Free