structured.py — langchain Source File
Architecture documentation for structured.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 6f140eef_7f84_2231_dd8c_bfae98be1932["structured.py"] 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b d45ff838_1238_0965_d3b7_5b33a0452ec0["langchain_core.output_parsers.json"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> d45ff838_1238_0965_d3b7_5b33a0452ec0 1f6af6bc_5660_a369_0bfb_4b5c8ed28743["pydantic.py"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> 1f6af6bc_5660_a369_0bfb_4b5c8ed28743 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> 91721f45_4909_e489_8c1f_084f8bd87145 8a49663d_c016_a6c1_e96e_621fdb612ef3["langchain_classic.output_parsers.format_instructions"] 6f140eef_7f84_2231_dd8c_bfae98be1932 --> 8a49663d_c016_a6c1_e96e_621fdb612ef3 style 6f140eef_7f84_2231_dd8c_bfae98be1932 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from __future__ import annotations
from typing import Any
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.output_parsers.json import parse_and_check_json_markdown
from pydantic import BaseModel
from typing_extensions import override
from langchain_classic.output_parsers.format_instructions import (
STRUCTURED_FORMAT_INSTRUCTIONS,
STRUCTURED_FORMAT_SIMPLE_INSTRUCTIONS,
)
line_template = '\t"{name}": {type} // {description}'
class ResponseSchema(BaseModel):
"""Schema for a response from a structured output parser."""
name: str
"""The name of the schema."""
description: str
"""The description of the schema."""
type: str = "string"
"""The type of the response."""
def _get_sub_string(schema: ResponseSchema) -> str:
return line_template.format(
name=schema.name,
description=schema.description,
type=schema.type,
)
class StructuredOutputParser(BaseOutputParser[dict[str, Any]]):
"""Parse the output of an LLM call to a structured output."""
response_schemas: list[ResponseSchema]
"""The schemas for the response."""
@classmethod
def from_response_schemas(
cls,
response_schemas: list[ResponseSchema],
) -> StructuredOutputParser:
"""Create a StructuredOutputParser from a list of ResponseSchema.
Args:
response_schemas: The schemas for the response.
Returns:
An instance of StructuredOutputParser.
"""
return cls(response_schemas=response_schemas)
def get_format_instructions(
self,
only_json: bool = False, # noqa: FBT001,FBT002
) -> str:
"""Get format instructions for the output parser.
Example:
```python
from langchain_classic.output_parsers.structured import (
StructuredOutputParser, ResponseSchema
)
response_schemas = [
ResponseSchema(
name="foo",
description="a list of strings",
type="List[string]"
),
ResponseSchema(
name="bar",
description="a string",
type="string"
),
]
parser = StructuredOutputParser.from_response_schemas(response_schemas)
print(parser.get_format_instructions()) # noqa: T201
output:
# The output should be a Markdown code snippet formatted in the following
# schema, including the leading and trailing "```json" and "```":
#
# ```json
# {
# "foo": List[string] // a list of strings
# "bar": string // a string
# }
# ```
Args:
only_json: If `True`, only the json in the Markdown code snippet
will be returned, without the introducing text.
"""
schema_str = "\n".join(
[_get_sub_string(schema) for schema in self.response_schemas],
)
if only_json:
return STRUCTURED_FORMAT_SIMPLE_INSTRUCTIONS.format(format=schema_str)
return STRUCTURED_FORMAT_INSTRUCTIONS.format(format=schema_str)
@override
def parse(self, text: str) -> dict[str, Any]:
expected_keys = [rs.name for rs in self.response_schemas]
return parse_and_check_json_markdown(text, expected_keys)
@property
def _type(self) -> str:
return "structured"
Domain
Subdomains
Functions
Dependencies
- langchain_classic.output_parsers.format_instructions
- langchain_core.output_parsers
- langchain_core.output_parsers.json
- pydantic.py
- typing
- typing_extensions
Source
Frequently Asked Questions
What does structured.py do?
structured.py is a source file in the langchain codebase, written in python. It belongs to the OutputParsing domain, StructuredValidation subdomain.
What functions are defined in structured.py?
structured.py defines 1 function(s): _get_sub_string.
What does structured.py depend on?
structured.py imports 6 module(s): langchain_classic.output_parsers.format_instructions, langchain_core.output_parsers, langchain_core.output_parsers.json, pydantic.py, typing, typing_extensions.
Where is structured.py in the architecture?
structured.py is located at libs/langchain/langchain_classic/output_parsers/structured.py (domain: OutputParsing, subdomain: StructuredValidation, directory: libs/langchain/langchain_classic/output_parsers).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free