fix: skip prompt_cache_key for Gemini provider

The prompt_cache_key field is accepted by most OpenAI-compatible
providers, but Gemini rejects unknown fields in the request body,
causing a 400 error.

This adds a supportPromptCache flag to the HTTP provider, enabled for
all providers except Gemini.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ubuntu 2026-02-26 03:06:56 +00:00
parent bee6f75b2c
commit 989594716c
2 changed files with 9 additions and 4 deletions

View file

@ -85,7 +85,7 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
apiBase = getDefaultAPIBase(protocol) apiBase = getDefaultAPIBase(protocol)
} }
p := NewHTTPProviderWithMaxTokensField(cfg.APIKey, apiBase, cfg.Proxy, cfg.MaxTokensField) p := NewHTTPProviderWithMaxTokensField(cfg.APIKey, apiBase, cfg.Proxy, cfg.MaxTokensField)
p.SetSupportPromptCache(true) // OpenAI supports prompt_cache_key p.SetSupportPromptCache(true)
return p, modelID, nil return p, modelID, nil
case "openrouter", "groq", "zhipu", "gemini", "nvidia", case "openrouter", "groq", "zhipu", "gemini", "nvidia",
@ -99,7 +99,12 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
if apiBase == "" { if apiBase == "" {
apiBase = getDefaultAPIBase(protocol) apiBase = getDefaultAPIBase(protocol)
} }
return NewHTTPProviderWithMaxTokensField(cfg.APIKey, apiBase, cfg.Proxy, cfg.MaxTokensField), modelID, nil p := NewHTTPProviderWithMaxTokensField(cfg.APIKey, apiBase, cfg.Proxy, cfg.MaxTokensField)
// Gemini rejects unknown fields in the request body; disable prompt_cache_key for it.
if protocol != "gemini" {
p.SetSupportPromptCache(true)
}
return p, modelID, nil
case "anthropic": case "anthropic":
if cfg.AuthMethod == "oauth" || cfg.AuthMethod == "token" { if cfg.AuthMethod == "oauth" || cfg.AuthMethod == "token" {

View file

@ -64,7 +64,7 @@ func NewProviderWithMaxTokensField(apiKey, apiBase, proxy, maxTokensField string
} }
// SetSupportPromptCache enables or disables sending the prompt_cache_key field. // SetSupportPromptCache enables or disables sending the prompt_cache_key field.
// Only OpenAI supports this field; other providers (e.g. Gemini) reject unknown fields. // Most providers accept or ignore this field, but Gemini rejects unknown fields.
func (p *Provider) SetSupportPromptCache(v bool) { func (p *Provider) SetSupportPromptCache(v bool) {
p.supportPromptCache = v p.supportPromptCache = v
} }
@ -122,7 +122,7 @@ func (p *Provider) Chat(
// with the same key and reuse prefix KV cache across calls. // with the same key and reuse prefix KV cache across calls.
// The key is typically the agent ID — stable per agent, shared across requests. // The key is typically the agent ID — stable per agent, shared across requests.
// See: https://platform.openai.com/docs/guides/prompt-caching // See: https://platform.openai.com/docs/guides/prompt-caching
// Only sent for providers that support it (e.g. OpenAI); others like Gemini reject unknown fields. // Disabled for providers like Gemini that reject unknown fields in the request body.
if p.supportPromptCache { if p.supportPromptCache {
if cacheKey, ok := options["prompt_cache_key"].(string); ok && cacheKey != "" { if cacheKey, ok := options["prompt_cache_key"].(string); ok && cacheKey != "" {
requestBody["prompt_cache_key"] = cacheKey requestBody["prompt_cache_key"] = cacheKey