Related Pages
Related topics: Structured LLM The following files were used as context for generating this wiki page:- src/starfish/llm/parser/pydantic_parser.py
- examples/structured_llm.ipynb
- src/starfish/llm/structured_llm.py
- src/starfish/llm/parser/json_parser.py
- tests/llm/parser/test_pydantic_parser.py
- tests/llm/parser/test_json_parser.py
Structured Outputs
Structured Outputs in Starfish provide a way to generate data in a predictable and type-safe format using Large Language Models (LLMs). This feature allows developers to define the structure of the output data using either Pydantic models or JSON schemas, ensuring that the generated data conforms to the specified format. This approach enhances the reliability and usability of synthetic data generated by LLMs.Overview of StructuredLLM
TheStructuredLLM
class is central to generating structured outputs. It takes a model name, a prompt, and an output schema as input. The output schema can be defined using Pydantic models or JSON schemas. The run
method of this class executes the LLM with the given prompt and parses the output into the specified structure. src/starfish/llm/structured_llm.py:16-41
Source File References
Core Implementation
- src/starfish/llm/structured_llm.py
- Main StructuredLLM class implementation
- Handles LLM execution and structured output generation
Parsers
- src/starfish/llm/parser/pydantic_parser.py
- Pydantic model parsing implementation
- Handles type validation and conversion
- src/starfish/llm/parser/json_parser.py
- JSON schema parsing implementation
- Includes schema conversion utilities
Tests
- tests/llm/parser/test_pydantic_parser.py
- Unit tests for Pydantic parser
- Covers various parsing scenarios
- tests/llm/parser/test_json_parser.py
- Unit tests for JSON parser
- Tests schema conversion and parsing
Examples
- examples/structured_llm.ipynb
- Example usage of StructuredLLM
- Demonstrates both Pydantic and JSON schema usage
Defining Output Schemas
Starfish supports two primary methods for defining output schemas: Pydantic models and JSON schemas.Pydantic Models
Pydantic models offer type safety and validation. By defining a Pydantic model, you can ensure that the generated data adheres to specific types and constraints. src/starfish/llm/parser/pydantic_parser.py:13-30JSON Schemas
JSON schemas provide a flexible way to define the structure of the output data. This method is useful when you need a more dynamic or less strict schema definition. src/starfish/llm/parser/json_parser.py:15-23Schema Conversion
TheJSONParser
includes functionality to convert a simplified field list to a JSON schema. src/starfish/llm/parser/json_parser.py:31-60 This allows for a more concise way to define schemas programmatically.
Parsing LLM Outputs
ThePydanticParser
and JSONParser
classes are responsible for parsing the output from LLMs and converting it into the specified structured format.
Pydantic Parser
ThePydanticParser
uses the Pydantic model to parse the LLM output. It handles various scenarios, including markdown code blocks. src/starfish/llm/parser/pydantic_parser.py:44-61
Example Usage
Here’s an example of how to useStructuredLLM
with a Pydantic model: