Output Modes
JSON Schema Mode
Learn how to use JSON Schema mode in Polyglot for structured and validated LLM responses.
JSON Schema mode takes JSON generation a step further by validating the response against a predefined schema. This ensures the response has exactly the structure your application expects.
Defining and Using a JSON Schema
Schema Validation
With JSON Schema mode, Polyglot ensures the LLM’s response adheres to your schema:
- The schema is sent to the model as part of the request
- The model structures its response to match the schema
- For providers with native schema support, validation happens at the API level
- For other providers, Polyglot helps guide the model to produce correctly formatted output
Provider Support for JSON Schema
Provider support for JSON Schema varies:
- OpenAI (GPT-4 and newer): Native support with
json_schema
response format - Anthropic (Claude 3 and newer): Partial support via prompt engineering
- Other providers: May require more explicit instructions in the prompt
For best compatibility, use OpenAI for schema-validated responses.
When to Use JSON Schema Mode
JSON Schema mode is ideal for:
- Applications requiring strictly typed data
- Integration with databases or APIs that expect specific structures
- Data extraction with complex nested structures
- Ensuring consistent response formats across multiple requests