google / generative-ai-go

Go SDK for Google Generative AI
Apache License 2.0
592 stars 59 forks source link

Provide a way to input raw JSON schema #220

Open areknoster opened 1 month ago

areknoster commented 1 month ago

Description of the feature request:

Provide a way to input JSON schema that's encoded in a raw form - so instead of having to

model := client.GenerativeModel("gemini-1.5-pro-002")
model.ResponseSchema = &genai.Schema{
    Type:        genai.TypeString,
}

I'd like to be able to

model := client.GenerativeModel("gemini-1.5-pro-002")
schema, err := genai.ParseRawSchema(`{"type": "string"}`)
if err != nil {
    return fmt.Errorf("incompatible or incorrect JSON schema: %w", err)
}
model.ResponseSchema = schema

I can think of following ways to solve this, but I think this would be the easiest one.

What problem are you trying to solve with this feature?

There are following usecases in which it's useful to have the response schema as raw JSON:

  1. It's a part of OpenAPI definition
  2. It leverages schema registry (e.g. kafka schema registry)
  3. It is used in other services supporting JSON schema - one major example can be GCP's own Data Stores
  4. One wants to experiment with given schema in the chat UI. In that case, thay can paste the schema in the chat settings. Currently this would involve manually translating in-code schema definition to JSON schema defintion.
  5. One wants to generate their types definitions based on schema using one of existing JSON Schema -> go types generators.
  6. One wants to additionally validate response against schema using existing tooling

Any other information you'd like to share?

No response