ai icon indicating copy to clipboard operation
ai copied to clipboard

AI Gateway: `cohere/embed-v4.0` intermittently ignores `outputDimension` (returns 1536 instead of requested 512)

Open scullum opened this issue 5 months ago • 2 comments

Summary

When requesting reduced-dimension embeddings from cohere/embed-v4.0 through the AI Gateway, the gateway intermittently ignores the requested output dimension and returns the model's native 1536-dim vector instead of the requested 512 (or 256/1024). The same byte-identical request returns the correct dimension in some windows and 1536 in others, toggling regardless of request shape — sometimes for sustained windows of many consecutive calls.

This is distinct from #8278 (which fixed the AI SDK forwarding providerOptions for embed()): here the client forwards the option correctly and the failure is server-side and specific to Cohere.

Environment

Reproduction

Both the typed SDK path and the OpenAI-compatible REST path reproduce it.

AI SDK:

import { embed } from 'ai';
import { createGateway } from '@ai-sdk/gateway';

const gateway = createGateway({ apiKey: process.env.AI_GATEWAY_API_KEY });
const { embedding } = await embed({
  model: gateway.textEmbeddingModel('cohere/embed-v4.0'),
  value: 'hello world',
  providerOptions: { cohere: { outputDimension: 512, inputType: 'search_document' } },
});
console.log(embedding.length); // expected 512; intermittently 1536

OpenAI-compatible REST (same result):

curl -s https://ai-gateway.vercel.sh/v1/embeddings \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"model":"cohere/embed-v4.0","input":"hello world","dimensions":512}' \
  | jq '{model, dim: (.data[0].embedding | length)}'
# -> {"model":"cohere/embed-v4.0","dim":1536}   (intermittently 512)

Run the curl repeatedly: the returned dim toggles between 512 and 1536 across calls with an unchanged request body. We've observed sustained windows of 6+ consecutive 1536 responses, and separate windows that correctly return 512 (and 1024 when dimensions: 1024 is requested).

Control — the gateway's dimension plumbing works for OpenAI but not Cohere

The identical OpenAI-compat call with an OpenAI embedding model honors dimensions deterministically:

curl -s https://ai-gateway.vercel.sh/v1/embeddings \
  -H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H 'Content-Type: application/json' \
  -d '{"model":"openai/text-embedding-3-small","input":"hello world","dimensions":768}' \
  | jq '{model, dim: (.data[0].embedding | length)}'
# -> dim: 768   (consistent across calls)

So the dimension auto-mapping is wired for OpenAI but appears not to be (reliably) wired for the Cohere provider on the gateway side.

Notes / evidence the request itself is correct

  • The option key is correct: @ai-sdk/cohere maps outputDimension → Cohere's output_dimension (see #12127). CamelCase outputDimension works when the gateway is in a honoring window; snake_case output_dimension in the request never works.
  • Fetch-intercepting the SDK confirms it sends providerOptions.cohere.outputDimension: 512 in the request body byte-for-byte identically in both the 512 and 1536 outcomes — the client is not the variable.
  • The 1536 responses still report model: cohere/embed-v4.0 (genuine embed-v4.0 output, not a fallback model).
  • All transports tested flip together at the same time (AI SDK /v3/ai/embedding-model and OpenAI-compat /v1/embeddings; with dimensions, output_dimension, root-level and nested) — pointing to a single server-side mapping issue rather than a client/API-shape problem.

Expected behavior

cohere/embed-v4.0 should deterministically return the requested output dimension (256/512/1024/1536), as it does for OpenAI embedding models.

Related

  • #8278 — AI Gateway: embed() ignores providerOptions when using embedding models (closed/fixed — the client-forwarding case)
  • #12127 — fix(amazon-bedrock/cohere): dimension option for cohere embedding models (confirms the outputDimension → output_dimension contract)

scullum avatar Jun 19 '26 16:06 scullum

im on it

Sarthak816 avatar Jun 22 '26 06:06 Sarthak816

Identify issue type

{
  "evidence": [
    "Requests to AI Gateway for `cohere/embed-v4.0` with `outputDimension`/`dimensions` sometimes return embeddings of length 1536 instead of requested 512/256/1024.",
    "Reporter provides SDK and OpenAI-compatible REST reproductions showing identical requests intermittently produce wrong dimensions.",
    "Control test says OpenAI embedding models honor `dimensions` consistently, suggesting Cohere gateway handling is broken.",
    "Expected behavior is deterministic return of the requested output dimension."
  ],
  "rationale": "The issue reports a concrete incorrect/intermittent behavior in the current AI Gateway embedding response for a supported model option, with reproducible examples and expected vs actual output.",
  "type": "bug",
  "confidence": "high"
}

lgrammel avatar Jul 02 '26 07:07 lgrammel