Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specify expected response type as JSON and pass schema #27

Open
taurit opened this issue Sep 10, 2024 · 1 comment
Open

Allow specify expected response type as JSON and pass schema #27

taurit opened this issue Sep 10, 2024 · 1 comment

Comments

@taurit
Copy link

taurit commented Sep 10, 2024

Is your feature request related to a problem? Please describe.

The most recent Gemini 1.5 API allows us to specify the exact JSON schema we expect for the response.

The SDK's current version doesn't allow users to request a response in JSON or pass the schema to the API.

Describe the solution you'd like

As a developer using the SDK, I would like to be able to specify the response type as JSON and provide the JSON schema:

var response = await geminiClient.TextPrompt(prompt, new GenerationConfig()
{
    MaxOutputTokens = 300,

    // Feature suggestion: add such property and pass the value to API as `response_mime_type`
    ResponseMimeType = "application/json",

    // Feature suggestion: add such property and pass the value to API as `response_schema`
    ResponseSchema = { ... // SDK could accept `JObject` or a string containing JSON}
});

Describe alternatives you've considered

Currently, I think the only alternative for this use case is to:

  • not use the SDK,
  • prepare a plain HTTP request using HttpClient or something similar.

Additional context

Google's documentation for ResponseMimeType and ResponseSchema doesn't contain examples of HTTP requests (only for SDKs in Python, Node, etc.).

I have experimented and provided examples of working requests to enable Gemini's JSON Mode on my blog.

@ndohertyjr
Copy link

ndohertyjr commented Jan 15, 2025

@taurit had a similar issue. In addition to the alternatives you've noted, a local class can extend the SDK's GenerationConfig class and be passed into the SDK to request a json response. That's what I'm doing for the moment but will try to submit a PR to add these properties if I have a moment.

public class GeminiGenerationConfig : GenerationConfig
{
    [JsonProperty("response_mime_type")]
    public string ResponseMimeType { get; set; } = "application/json";
    
    // Continue here by adding ResponseSchema with Json Property name 'response_schema'
}

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

No branches or pull requests

2 participants