StructuredOutputParser Class — langchain Architecture
Architecture documentation for the StructuredOutputParser class in structured.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 978d47b2_462d_e3f1_a915_43e804fd6b61["StructuredOutputParser"] 6f140eef_7f84_2231_dd8c_bfae98be1932["structured.py"] 978d47b2_462d_e3f1_a915_43e804fd6b61 -->|defined in| 6f140eef_7f84_2231_dd8c_bfae98be1932 aa3597db_802e_6a26_3f4c_43a29d1c41c7["from_response_schemas()"] 978d47b2_462d_e3f1_a915_43e804fd6b61 -->|method| aa3597db_802e_6a26_3f4c_43a29d1c41c7 fdc0557a_7650_ed36_a9a7_54a74bd977a2["get_format_instructions()"] 978d47b2_462d_e3f1_a915_43e804fd6b61 -->|method| fdc0557a_7650_ed36_a9a7_54a74bd977a2 a1830a88_5662_8d16_1e14_60e6328e0235["parse()"] 978d47b2_462d_e3f1_a915_43e804fd6b61 -->|method| a1830a88_5662_8d16_1e14_60e6328e0235 cf7e5cc0_44c4_413a_1ab3_beaf2cd82ac6["_type()"] 978d47b2_462d_e3f1_a915_43e804fd6b61 -->|method| cf7e5cc0_44c4_413a_1ab3_beaf2cd82ac6
Relationship Graph
Source Code
libs/langchain/langchain_classic/output_parsers/structured.py lines 37–116
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
Source
Frequently Asked Questions
What is the StructuredOutputParser class?
StructuredOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/output_parsers/structured.py.
Where is StructuredOutputParser defined?
StructuredOutputParser is defined in libs/langchain/langchain_classic/output_parsers/structured.py at line 37.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free