Open
Description
Is your feature request related to a problem? Please describe.
The current structured output feature in the Go SDK requires passing a Go struct type directly to ai.WithOutputType()
. This creates significant challenges when building distributed systems where the LLM inference service is separate from client services:
// Current approach - works only when struct is available
type MenuItem struct {
Name string `json:"name"`
Description string `json:"description"`
Calories int `json:"calories"`
Allergens []string `json:"allergens"`
}
resp, err := genkit.Generate(ctx, g,
ai.WithPrompt("Invent a menu item for a pirate themed restaurant."),
ai.WithOutputType(MenuItem{}), // Requires the struct to be defined in this service
)
Problems with this approach:
- Service decoupling issues: Client services must marshal their struct definitions to send to the LLM service
- Type reconstruction complexity: The LLM service must reconstruct Go struct types from marshaled data using custom functions
- Maintenance burden: Schema changes require updates across multiple services
- Language interoperability: Non-Go services cannot easily consume this API
Describe the solution you'd like
Add support for JSON Schema as an alternative to Go struct types for structured output. This would allow:
// Proposed approach - using JSON Schema string
jsonSchema := `{
"type": "object",
"properties": {
"name": {"type": "string"},
"description": {"type": "string"},
"calories": {"type": "integer"},
"allergens": {"type": "array", "items": {"type": "string"}}
},
"required": ["name", "description", "calories"]
}`
resp, err := genkit.Generate(ctx, g,
ai.WithPrompt("Invent a menu item for a pirate themed restaurant."),
ai.WithOutputSchema(jsonSchema), // New function accepting JSON Schema
)
Benefits:
- Schema can be easily transmitted between services as a string
- No need for type reconstruction in the LLM service
- Better support for microservice architectures
- Language-agnostic schema definitions
- Maintains existing struct-based API for backward compatibility
Describe alternatives you've considered
- Custom marshaling functions: Currently implementing workarounds to reconstruct struct types from marshaled data, but this is error-prone and complex
- Shared libraries: Creating shared Go modules with common types, but this creates tight coupling between services
- Code generation: Generating Go structs from schema definitions, but this adds build complexity
Additional context
This feature would be particularly valuable for:
- Microservice architectures where LLM inference is centralized
- Multi-language environments where non-Go services need structured output
- Dynamic schema scenarios where output structure is determined at runtime
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
No status