Home / Class/ ProviderStrategyBinding Class — langchain Architecture

ProviderStrategyBinding Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  7ab19be5_f449_5f71_c0e1_871dfc8e0588["ProviderStrategyBinding"]
  c2936c24_74cd_6911_037b_0f67eebfefee["structured_output.py"]
  7ab19be5_f449_5f71_c0e1_871dfc8e0588 -->|defined in| c2936c24_74cd_6911_037b_0f67eebfefee
  9d9e56c6_1440_e68e_5bbf_e3962aa21d22["from_schema_spec()"]
  7ab19be5_f449_5f71_c0e1_871dfc8e0588 -->|method| 9d9e56c6_1440_e68e_5bbf_e3962aa21d22
  0fda5ec1_1f54_5091_4c3c_0202a78dacb3["parse()"]
  7ab19be5_f449_5f71_c0e1_871dfc8e0588 -->|method| 0fda5ec1_1f54_5091_4c3c_0202a78dacb3
  43c7c9c7_b2c0_1dcc_4be8_a8f880c39f25["_extract_text_content_from_message()"]
  7ab19be5_f449_5f71_c0e1_871dfc8e0588 -->|method| 43c7c9c7_b2c0_1dcc_4be8_a8f880c39f25

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/structured_output.py lines 363–444

class ProviderStrategyBinding(Generic[SchemaT]):
    """Information for tracking native structured output metadata.

    This contains all necessary information to handle structured responses generated via
    native provider output, including the original schema, its type classification, and
    parsing logic for provider-enforced JSON.
    """

    schema: type[SchemaT] | dict[str, Any]
    """The original schema provided for structured output (Pydantic model, `dataclass`,
    `TypedDict`, or JSON schema dict).
    """

    schema_kind: SchemaKind
    """Classification of the schema type for proper response construction."""

    @classmethod
    def from_schema_spec(cls, schema_spec: _SchemaSpec[SchemaT]) -> Self:
        """Create a `ProviderStrategyBinding` instance from a `SchemaSpec`.

        Args:
            schema_spec: The `SchemaSpec` to convert

        Returns:
            A `ProviderStrategyBinding` instance for parsing native structured output
        """
        return cls(
            schema=schema_spec.schema,
            schema_kind=schema_spec.schema_kind,
        )

    def parse(self, response: AIMessage) -> SchemaT:
        """Parse `AIMessage` content according to the schema.

        Args:
            response: The `AIMessage` containing the structured output

        Returns:
            The parsed response according to the schema

        Raises:
            ValueError: If text extraction, JSON parsing or schema validation fails
        """
        # Extract text content from AIMessage and parse as JSON
        raw_text = self._extract_text_content_from_message(response)

        try:
            data = json.loads(raw_text)
        except Exception as e:
            schema_name = getattr(self.schema, "__name__", "response_format")
            msg = (
                f"Native structured output expected valid JSON for {schema_name}, "
                f"but parsing failed: {e}."
            )
            raise ValueError(msg) from e

        # Parse according to schema
        return _parse_with_schema(self.schema, self.schema_kind, data)

    @staticmethod
    def _extract_text_content_from_message(message: AIMessage) -> str:
        """Extract text content from an `AIMessage`.

        Args:
            message: The AI message to extract text from

        Returns:
            The extracted text content
        """
        content = message.content
        if isinstance(content, str):
            return content
        parts: list[str] = []
        for c in content:
            if isinstance(c, dict):
                if c.get("type") == "text" and "text" in c:
                    parts.append(str(c["text"]))
                elif "content" in c and isinstance(c["content"], str):
                    parts.append(c["content"])
            else:
                parts.append(str(c))

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free