Skip to content

[Go] Support JSON Schema for Structured Output #3149

Open
@Amin-nem

Description

@Amin-nem

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:

  1. Service decoupling issues: Client services must marshal their struct definitions to send to the LLM service
  2. Type reconstruction complexity: The LLM service must reconstruct Go struct types from marshaled data using custom functions
  3. Maintenance burden: Schema changes require updates across multiple services
  4. 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

  1. Custom marshaling functions: Currently implementing workarounds to reconstruct struct types from marshaled data, but this is error-prone and complex
  2. Shared libraries: Creating shared Go modules with common types, but this creates tight coupling between services
  3. 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

No one assigned

    Labels

    featureNew feature or requestgo

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions