Skip to content

fix(openai_compat): improve prompt_cache_key host matching#1387

Merged
yinwm merged 2 commits intosipeed:mainfrom
mahendrateja95:fix/prompt-cache-key-non-openai-providers
Mar 11, 2026
Merged

fix(openai_compat): improve prompt_cache_key host matching#1387
yinwm merged 2 commits intosipeed:mainfrom
mahendrateja95:fix/prompt-cache-key-non-openai-providers

Conversation

@mahendrateja95
Copy link
Contributor

Follow-up to #1353, addressing reviewer feedback from @yinwm:

  1. Replaced strings.Contains(apiBase, "api.openai.com") with proper url.Parse() +
    url.Hostname() matching to prevent false positives from proxy URLs
    (e.g., https://proxy.com/api.openai.com/v1 would incorrectly match before this fix).

  2. Added Azure OpenAI endpoint support (*.openai.azure.com) to the allowlist, since
    Azure OpenAI also supports prompt_cache_key.

  3. Refactored test helper to reduce duplication in cache key test cases.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

AI provided the initial code draft. I reviewed, modified, and validated the changes.

🔗 Related Issue

Follow-up to #1353. Fixes #1333.

📚 Technical Context

  • Reference URL:
    https://platform.openai.com/docs/guides/prompt-caching
  • Reasoning:
    • strings.Contains() is unsafe — proxy URLs like https://proxy.com/api.openai.com/v1
      would false-positive on the substring match
    • url.Parse() + url.Hostname() extracts the actual host for reliable matching
    • Azure OpenAI (*.openai.azure.com) supports prompt caching and was missing from the
      original allowlist
    • New supportsPromptCacheKey() helper keeps the logic clean and testable

🧪 Test Environment

  • Hardware: PC
  • OS: Windows 11
  • Model/Provider: Tested with unit tests against Mistral, Gemini, DeepSeek, Groq,
    Minimax, Ollama, OpenAI, and Azure OpenAI API bases
  • Channels: N/A (provider-level fix)

📸 Evidence

Details Tests added/updated:
  • TestProviderChat_PromptCacheKeySentToOpenAI — verifies OpenAI still gets the field
  • TestProviderChat_PromptCacheKeyOmittedForNonOpenAI — 6 non-OpenAI providers excluded
  • TestSupportsPromptCacheKey — edge cases: Azure variants, proxy URLs, malformed inputs, empty strings

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

mahendrateja95 and others added 2 commits March 12, 2026 00:02
Azure OpenAI (*.openai.azure.com) also supports prompt_cache_key, so
extend the allowlist to match Azure endpoints alongside api.openai.com.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Replace strings.Contains with url.Parse + exact host comparison to
prevent false positives from proxy URLs or path segments. Extract
chatWithCacheKey test helper to reduce duplication. Add edge-case
tests for proxy URLs, empty strings, and malformed input.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Copy link
Collaborator

@yinwm yinwm left a comment

Choose a reason for hiding this comment

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

LGTM! The changes improve the robustness of prompt_cache_key host matching and add Azure OpenAI support.

Key improvements:

  1. Uses url.Parse() + Hostname() instead of strings.Contains() to prevent false positives from proxy URLs
  2. Adds Azure OpenAI (*.openai.azure.com) support
  3. Good test coverage for edge cases (proxy URLs, empty strings, malformed input)

Thanks for the contribution!

@yinwm yinwm merged commit 6612ca0 into sipeed:main Mar 11, 2026
4 checks passed
dj-oyu pushed a commit to dj-oyu/picoclaw that referenced this pull request Mar 14, 2026
LGTM! The changes improve the robustness of prompt_cache_key host matching and add Azure OpenAI support. Thanks for the contribution!
dj-oyu pushed a commit to dj-oyu/picoclaw that referenced this pull request Mar 16, 2026
LGTM! The changes improve the robustness of prompt_cache_key host matching and add Azure OpenAI support. Thanks for the contribution!
j0904 pushed a commit to j0904/picoclaw that referenced this pull request Mar 22, 2026
LGTM! The changes improve the robustness of prompt_cache_key host matching and add Azure OpenAI support. Thanks for the contribution!
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.

[BUG]

2 participants