Principle:24kchengYe MemoMind Structured Output Generation
| Knowledge Sources | |
|---|---|
| Domains | LLM_Engineering, Structured_Prediction |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Technique that extracts structured data conforming to a JSON Schema from free-form LLM answers by generating a dynamic Pydantic model and making a secondary extraction call.
Description
When a system needs a structured response (e.g., a JSON object with specific fields) from an LLM that produced a free-form text answer, structured output generation bridges the gap. It reads the JSON Schema, builds a dynamic Pydantic model from its properties, and prompts the LLM to extract the relevant fields from the answer text.
This two-stage approach (free-form answer → structured extraction) is more reliable than forcing the LLM to produce structured output directly, because the reasoning that produces the answer is decoupled from the formatting constraint.
Usage
Use this principle when an LLM agent's answer must be consumed programmatically as structured data, but the reasoning quality benefits from free-form generation first.
Theoretical Basis
# Abstract structured output (not real implementation)
DynamicModel = pydantic.create_model("StructuredResponse", **{
field: (json_type_to_python(schema), default)
for field, schema in response_schema["properties"].items()
})
prompt = f"Extract fields from this answer:\n{answer}\nFormat: {schema}"
result = llm.call(messages=[...], response_format=DynamicModel)
structured_output = result.model_dump()
The JSON Schema type mapping: string→str, integer→int, number→float, boolean→bool, array→list, object→dict. Required fields are marked with `...` (Pydantic's required sentinel).