This example demonstrates how to use JSON Schema support in gollem to get structured JSON output from LLM providers (OpenAI, Claude, and Gemini).
- Define a structured schema for LLM responses
- Extract structured data from natural language
- Support for nested objects and arrays
- Field validation with constraints
- Works across multiple LLM providers
Set the appropriate environment variables for the providers you want to use:
# For OpenAI
export OPENAI_API_KEY="your-openai-api-key"
# For Claude
export ANTHROPIC_API_KEY="your-anthropic-api-key"
# For Gemini
export GEMINI_PROJECT_ID="your-gcp-project-id"
export GEMINI_LOCATION="us-central1" # or your preferred locationgo run main.goThe example will try to run all three providers. If an API key is not set, that provider will be skipped.
The example defines a UserProfile schema with the following fields:
name(string, required): Full name of the userage(integer): Age in years (0-150)email(string, required): Email addressinterests(array of strings): List of hobbies or interestslocation(object): User's location withcityandcountryfields
schema := &gollem.Parameter{
Title: "UserProfile",
Description: "Structured user profile information",
Type: gollem.TypeObject,
Properties: map[string]*gollem.Parameter{
"name": {
Type: gollem.TypeString,
Description: "Full name of the user",
},
"age": {
Type: gollem.TypeInteger,
Description: "Age in years",
Minimum: Ptr(0.0),
Maximum: Ptr(150.0),
},
// ... more fields
},
Required: []string{"name", "email"},
}Create a session with JSON content type and response schema:
session, err := client.NewSession(ctx,
gollem.WithSessionContentType(gollem.ContentTypeJSON),
gollem.WithSessionResponseSchema(schema),
)Send a natural language prompt and receive structured JSON:
resp, err := session.GenerateContent(ctx,
gollem.Text("Extract user information: Sarah Johnson is 28 years old, email: sarah.j@example.com, lives in Seattle, USA, and enjoys hiking, photography, and cooking."))
// Response will be a valid JSON matching the schema- Uses Structured Outputs with
response_format.json_schema - Uses strict mode (internal default:
strict=falsefor flexibility) - Requires
gpt-4o-2024-08-06or later models - Constrained decoding ensures high schema adherence
- Uses system prompt + prefill technique
- Schema is embedded in the system prompt
- Prefills response with
{to enforce JSON format - May have slightly lower accuracy than native schema support
- Uses
ResponseSchemaAPI parameter - Native schema support via Vertex AI
- Works with Gemini 1.5 Pro and Flash models
- Complex schemas may trigger
InvalidArgument: 400errors
{
"name": "Sarah Johnson",
"age": 28,
"email": "sarah.j@example.com",
"interests": [
"hiking",
"photography",
"cooking"
],
"location": {
"city": "Seattle",
"country": "USA"
}
}The example demonstrates several schema features:
- Required fields:
nameandemailmust be present - Type validation: Each field has a specific type
- Nested objects:
locationis an object with sub-fields - Arrays:
interestsis an array of strings - Constraints:
agehas minimum and maximum values - Descriptions: Each field has a description for the LLM
The example includes proper error handling for:
- Missing API keys
- Session creation failures
- Content generation errors
- JSON parsing errors