SGR complete guide — code examples, patterns, structured output
Key Takeaways
1855-line reference compiling Rinat Abdullin’s Schema-Guided Reasoning (SGR) series. Core: use Pydantic/Zod schemas as the interface between LLM and code — the model generates structured output conforming to typed schemas, replacing brittle string parsing.
Core pattern: define Pydantic BaseModel → pass as response_format to OpenAI → get typed object back. No JSON parsing, no regex, no “hoping the model returns valid output.”
Tool calling via SGR: define tools as Union[ToolA, ToolB, …]. Model picks one, returns typed object. Dispatch is a simple match/case. Parallel execution: action: List[Union[...]] returns multiple tool calls at once.
Cycle pattern: LLM → structured output → execute tools → feed results back → LLM reasons again → next action. Repeats until “done” or max iterations. This is the foundation of agentic workflows.
Risk analysis example: define RiskFactor schema with severity enum. Model fills list of risks with explanations. Structured output = auditable, testable, storable.
For our stack: Pydantic (Python), Zod (TypeScript), @Generable (Swift/Apple Foundation Models). Agent reads schemas in Models/schemas/types/ before any work.
Connections
- dev-principles-summary — SGR is a core section of dev principles
- apple-on-device-ai — @Generable = native SGR in Swift
- context-engineering — schemas ARE context: agent reads them first to understand domain
- enterprise-rag-challenge — ERC winners validate SGR: structured outputs beat vector search
- tool-calling-four-layers — tool calling uses the same constrained decoding as SGR structured outputs
Practical: use /sgr skill in solo-factory to design SGR pipelines. Rust: sgr-agent crate. Python: sgr-agent-core.