Skip to content

Conversation

@DragonFSKY
Copy link

Summary

Add support for Google AI's native structured output feature using ResponseSchema. This enables users to define JSON schemas that the model will strictly follow when generating responses.

Partially addresses #1342

Changes

  • Add ResponseSchema field to llms.CallOptions for structured output support
  • Add WithResponseSchema() helper function in llms package
  • Export Schema type and Type* constants from googleai package for convenience
  • Auto-set ResponseMIMEType to "application/json" when using schema
  • Add validation for MIME type compatibility (returns error if incompatible)
  • Add comprehensive unit tests for schema types and options

Usage

import (
    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/llms/googleai"
)

schema := &googleai.Schema{
    Type: googleai.TypeObject,
    Properties: map[string]*googleai.Schema{
        "name":  {Type: googleai.TypeString, Description: "User name"},
        "age":   {Type: googleai.TypeInteger, Description: "User age"},
        "roles": {Type: googleai.TypeArray, Items: &googleai.Schema{Type: googleai.TypeString}},
    },
    Required: []string{"name"},
}

resp, err := llm.GenerateContent(ctx, messages,
    llms.WithResponseSchema(schema),
)

Implementation Details

  • Uses Google's native ResponseSchema field in GenerationConfig (not prompt-based JSON mode)
  • Type-safe: Returns explicit error if wrong schema type is passed
  • Backward compatible: No changes to existing API behavior
  • Follows the same pattern as existing WithResponseMIMEType() and WithJSONMode() options

Testing

  • All existing unit tests pass
  • Added new tests for:
    • Schema type aliases and constants
    • WithResponseSchema option functionality
    • Backward compatibility with existing options
    • Complex nested schema structures

Add support for Google AI's native structured output feature using
ResponseSchema. This enables users to define JSON schemas that the
model will strictly follow when generating responses.

Changes:
- Add ResponseSchema field to llms.CallOptions
- Add WithResponseSchema() helper function
- Export Schema type and Type* constants from googleai package
- Auto-set ResponseMIMEType to "application/json" when using schema
- Add validation for MIME type compatibility
- Add comprehensive unit tests

Partially addresses tmc#1342
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant