_SchemaSpec Class — langchain Architecture
Architecture documentation for the _SchemaSpec class in structured_output.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 7a6d9b97_f989_1429_b606_bd953134d970["_SchemaSpec"] c2936c24_74cd_6911_037b_0f67eebfefee["structured_output.py"] 7a6d9b97_f989_1429_b606_bd953134d970 -->|defined in| c2936c24_74cd_6911_037b_0f67eebfefee eeee1ad6_c65f_c163_81ee_3b764b9352ba["__init__()"] 7a6d9b97_f989_1429_b606_bd953134d970 -->|method| eeee1ad6_c65f_c163_81ee_3b764b9352ba
Relationship Graph
Source Code
libs/langchain_v1/langchain/agents/structured_output.py lines 106–191
class _SchemaSpec(Generic[SchemaT]):
"""Describes a structured output schema."""
schema: type[SchemaT] | dict[str, Any]
"""The schema for the response, can be a Pydantic model, `dataclass`, `TypedDict`,
or JSON schema dict.
"""
name: str
"""Name of the schema, used for tool calling.
If not provided, the name will be the class name for models/dataclasses/TypedDicts,
or the `title` field for JSON schemas.
Falls back to a generated name if unavailable.
"""
description: str
"""Custom description of the schema.
If not provided, will use the model's docstring.
"""
schema_kind: SchemaKind
"""The kind of schema."""
json_schema: dict[str, Any]
"""JSON schema associated with the schema."""
strict: bool | None = None
"""Whether to enforce strict validation of the schema."""
def __init__(
self,
schema: type[SchemaT] | dict[str, Any],
*,
name: str | None = None,
description: str | None = None,
strict: bool | None = None,
) -> None:
"""Initialize `SchemaSpec` with schema and optional parameters.
Args:
schema: Schema to describe.
name: Optional name for the schema.
description: Optional description for the schema.
strict: Whether to enforce strict validation of the schema.
Raises:
ValueError: If the schema type is unsupported.
"""
self.schema = schema
if name:
self.name = name
elif isinstance(schema, dict):
self.name = str(schema.get("title", f"response_format_{str(uuid.uuid4())[:4]}"))
else:
self.name = str(getattr(schema, "__name__", f"response_format_{str(uuid.uuid4())[:4]}"))
self.description = description or (
schema.get("description", "")
if isinstance(schema, dict)
else getattr(schema, "__doc__", None) or ""
)
self.strict = strict
if isinstance(schema, dict):
self.schema_kind = "json_schema"
self.json_schema = schema
elif isinstance(schema, type) and issubclass(schema, BaseModel):
self.schema_kind = "pydantic"
self.json_schema = schema.model_json_schema()
elif is_dataclass(schema):
self.schema_kind = "dataclass"
self.json_schema = TypeAdapter(schema).json_schema()
elif is_typeddict(schema):
self.schema_kind = "typeddict"
self.json_schema = TypeAdapter(schema).json_schema()
else:
Source
Frequently Asked Questions
What is the _SchemaSpec class?
_SchemaSpec is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/structured_output.py.
Where is _SchemaSpec defined?
_SchemaSpec is defined in libs/langchain_v1/langchain/agents/structured_output.py at line 106.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free