Skip to content

fix: forward cachedContent parameter in OpenAI-to-Gemini transformation#5605

Open
colegottdank wants to merge 1 commit intomainfrom
fix/gemini-cached-content-mapping
Open

fix: forward cachedContent parameter in OpenAI-to-Gemini transformation#5605
colegottdank wants to merge 1 commit intomainfrom
fix/gemini-cached-content-mapping

Conversation

@colegottdank
Copy link
Collaborator

Summary

  • Add cachedContent field to GeminiGenerateContentRequest interface
  • Forward cachedContent from OpenAI-format requests to Gemini native format
  • Support both Vertex AI format (projects/*/locations/*/cachedContents/*) and Google AI Studio format (cachedContents/*)

Problem

When users send requests through the AI Gateway with explicit caching:

{
  "model": "gemini-3-flash-preview/vertex",
  "messages": [{"role": "user", "content": "Analyze the data in the cache."}],
  "cachedContent": "projects/YOUR_PROJECT/locations/us-central1/cachedContents/YOUR_CACHE_ID"
}

The cachedContent parameter was being dropped during the OpenAI-to-Gemini transformation, causing:

  1. Gemini not using the cached content
  2. Higher token usage (all tokens counted as fresh input)
  3. Higher costs (no cache discount applied)

Solution

The toGoogle() transformation function now forwards the cachedContent parameter to the native Gemini request format.

Test plan

  • Added 5 tests for cachedContent transformation
  • Full packages test suite passes (583 tests)

🤖 Generated with Claude Code

- Add cachedContent field to GeminiGenerateContentRequest interface
- Forward cachedContent from OpenAI-format requests to Gemini native format
- Support both Vertex AI format (projects/*/locations/*/cachedContents/*)
  and Google AI Studio format (cachedContents/*)

This fixes explicit caching not working when users pass cachedContent
through the AI Gateway with OpenAI-compatible requests.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 24, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
helicone Error Error Feb 24, 2026 7:27am
helicone-bifrost Ready Ready Preview, Comment Feb 24, 2026 7:27am
helicone-eu Error Error Feb 24, 2026 7:27am

Request Review

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@supabase
Copy link

supabase bot commented Feb 24, 2026

This pull request has been ignored for the connected project bolqqmqbrciybnypvklh because there are no changes detected in supbase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@supabase
Copy link

supabase bot commented Feb 24, 2026

This pull request has been ignored for the connected project lmahfbbnchpworytrrqk because there are no changes detected in .supabase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@claude
Copy link
Contributor

claude bot commented Feb 24, 2026

Claude finished @colegottdank's task —— View job


PR Review Complete ✅

Score: 9/10 - Excellent implementation with comprehensive tests, no critical issues found

Code Review Analysis

Changes Overview:

  • Added cachedContent?: string to GeminiGenerateContentRequest interface
  • Updated toGoogle() function to forward cachedContent parameter from OpenAI requests
  • Added comprehensive test coverage with 5 test cases

Strengths

  1. Clean Implementation: The solution is minimal and focused, adding only the necessary code to solve the problem
  2. Type Safety: Proper TypeScript interfaces with clear documentation
  3. Comprehensive Tests: 5 well-designed test cases covering:
    • Both Vertex AI format (projects/*/locations/*/cachedContents/*)
    • Google AI Studio format (cachedContents/*)
    • Edge cases (undefined, empty string)
    • Integration with other parameters
  4. Non-Breaking: Only adds functionality when cachedContent is present
  5. Good Documentation: Clear comments explaining supported formats

🔍 Code Quality Assessment

toGoogle.ts:111-117 - The implementation is solid:

const cachedContent = (openAIBody as any).cachedContent;
if (typeof cachedContent === "string" && cachedContent.length > 0) {
  geminiBody.cachedContent = cachedContent;
}
  • Proper type checking with typeof and length validation
  • Clean conditional logic that prevents empty strings

google.ts:73-85 - Interface definition is well-documented:

/**
 * Reference to a cached content resource.
 * Format: "projects/{project}/locations/{location}/cachedContents/{cache_id}"
 * or "cachedContents/{cache_id}" for Google AI Studio
 */
cachedContent?: string;

💡 Minor Suggestion

Consider using explicit typing instead of (openAIBody as any).cachedContent in toGoogle.ts:114:

// Current
const cachedContent = (openAIBody as any).cachedContent;

// Suggested (if feasible)
const cachedContent = (openAIBody as HeliconeChatCreateParams & { cachedContent?: string }).cachedContent;

However, this change isn't critical since the current approach works correctly and maintains backward compatibility.

🚀 Impact Assessment

  • Problem Solved: Fixes the issue where cachedContent was being dropped during transformation
  • Cost Savings: Will prevent unnecessary token charges when using cached content
  • Compatibility: Supports both Vertex AI and Google AI Studio formats
  • Reliability: Comprehensive test coverage ensures the feature works as expected

Recommendation

Approve and merge - This is a well-implemented fix that solves a real cost-optimization problem with proper test coverage and no breaking changes.


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