Skip to content

Fix Cohere provider-prefixed model routing#22

Merged
parteeksingh24 merged 2 commits into
mainfrom
fix/cohere-prefix-routing
Jun 17, 2026
Merged

Fix Cohere provider-prefixed model routing#22
parteeksingh24 merged 2 commits into
mainfrom
fix/cohere-prefix-routing

Conversation

@parteeksingh24

@parteeksingh24 parteeksingh24 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Rewrite and route public cohere/ and deepseek/ model IDs correctly, from one shared prefix list.

  • Strip cohere/ public model prefixes before forwarding to Cohere
  • Detect cohere/ and deepseek/ providers by name from that same list
  • Add AutoRouter regression coverage for a prefixed Cohere catalog ID
  • Update the gated AI Gateway live regression to cover the prefixed ID shape
  • Add a drift guard so the strip list and routing list can't diverge again

Why

When AI Gateway sends a public model ID like cohere/command-a-plus-05-2026, Cohere's compatibility API expects the native model ID:

{
  "model": "command-a-plus-05-2026"
}

AutoRouter keeps provider-prefix knowledge in one map, knownProviderPrefixes, used for two jobs:

  • Stripping (stripProviderPrefix) rewrites cohere/xx before forwarding.
  • Routing (DetectProviderFromModel) maps cohere/x → provider cohere.

These previously read two separate hand-maintained lists. cohere (this PR) and deepseek (an earlier PR) were added to the strip list but never the routing switch, so a direct consumer sending cohere/... with no X-Provider header got ErrNoProvider. Through AI Gateway the gap was masked (Ion sets X-Provider and carries its own prefix fallback), so it was latent rather than a live outage.

flowchart TD
    R["model: cohere/command-a-plus-05-2026"] --> H{"X-Provider header set?"}
    H -- yes --> U["use header value"]
    H -- no --> D["DetectProviderFromModel(model)"]
    D --> M{"prefix in knownProviderPrefixes?"}
    M -- yes --> PV["provider = cohere"]
    M -- no --> NH["name heuristics, else ErrNoProvider"]
    PV --> SP["stripProviderPrefix() reads same map"]
    SP --> UP["upstream gets command-a-plus-05-2026"]
Loading

Implementation

Cohere stays in the central prefix-stripping path used by providers such as DeepSeek:

var knownProviderPrefixes = map[string]bool{
    "deepseek": true,
    "cohere":   true,
}

DetectProviderFromModel now reads that same map instead of a second hardcoded switch, so a prefix the router strips is always a prefix it can route:

// detection.go
if idx := strings.Index(model, "/"); idx >= 0 {
    if prefix := model[:idx]; knownProviderPrefixes[prefix] {
        return prefix
    }
}

A new guard asserts every stripped prefix is routable, so the two lists can't silently drift again:

func TestDetectProviderFromModel_KnownPrefixesAreDetected(t *testing.T) {
    for prefix := range knownProviderPrefixes {
        model := prefix + "/some-model"
        if got := DetectProviderFromModel(model); got != prefix {
            t.Errorf("DetectProviderFromModel(%q) = %q, want %q", model, got, prefix)
        }
    }
}

The AutoRouter regression still exercises AutoRouter.Forward and asserts the fake Cohere upstream receives the stripped model, and the gated live regression still uses the public ID shape cohere/command-a-plus-05-2026.

Verification

Prefix-routing change (this commit):

  • go test . -run 'TestDetectProviderFromModel|TestDefaultProviderDetector|TestAutoRouter_Cohere|TestStripProviderPrefix' -count=1
  • go build ./... and go vet ./...
  • go test ./... -count=1

Carried from the strip commit (re-run after stacking before merge):

  • go test -race ./... -count=1
  • go test -tags integration ./... -run 'TestLiveAIGatewayRegressionModels' -count=1
  • make test blocked locally at govulncheck because local Go is go1.26.2 and the stdlib vuln gate requires a newer Go patch version

Summary by CodeRabbit

  • Bug Fixes

    • Provider-prefixed Cohere model names are now correctly stripped before forwarding requests to upstream services.
  • Tests

    • Added regression tests to verify provider prefix stripping behavior for Cohere models.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Refactors provider detection to use knownProviderPrefixes as the single source of truth for explicit provider-prefix matching, adds "cohere" to that map, and verifies prefix detection and stripping through unit tests and a live regression test using the cohere/command-a-plus-05-2026 model.

Changes

Cohere Provider Prefix Stripping and Detection Unification

Layer / File(s) Summary
Provider detection refactor to use knownProviderPrefixes
detection.go, detection_test.go
DetectProviderFromModel replaces hard-coded prefix switch with knownProviderPrefixes map lookup, and tests verify correct detection for cohere/ and deepseek/ prefixes plus all registered known prefixes.
Cohere prefix registration and unit tests
autorouter.go, autorouter_test.go
Adds "cohere": true to knownProviderPrefixes, introduces TestAutoRouter_CohereStripsProviderPrefixBeforeForwarding verifying upstream receives command-a-plus-05-2026, and extends TestStripProviderPrefix with a cohere test vector.
Live regression test update
aigateway_regression_live_test.go
Updates cohere live test case name and model from command-r-08-2024 to cohere/command-a-plus-05-2026 to exercise end-to-end prefix stripping.
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@parteeksingh24 parteeksingh24 marked this pull request as ready for review June 15, 2026 17:30
- `DetectProviderFromModel` now reads `knownProviderPrefixes`
- Routes `cohere/` and `deepseek/` IDs, not `ErrNoProvider`
- Removes the duplicate prefix `switch` that had drifted
- Adds test asserting every stripped prefix is routable
@parteeksingh24 parteeksingh24 requested a review from jhaynie June 17, 2026 18:06
@parteeksingh24 parteeksingh24 merged commit edac237 into main Jun 17, 2026
2 checks passed
@parteeksingh24 parteeksingh24 deleted the fix/cohere-prefix-routing branch June 17, 2026 18:44
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.

2 participants