[JS] [compat-oai] visualDetailLevel leaks into OpenAI API request body causing "Unknown parameter" error
Describe the bug When using @genkit-ai/compat-oai with visualDetailLevel in the config, the parameter is correctly used to set the detail field on image URLs in toOpenAIMessages(), but it also leaks into the API request body via restOfConfig, causing OpenAI to reject the request with:
400 Unknown parameter: 'visualDetailLevel'
To Reproduce
import { openAI } from '@genkit-ai/compat-oai/openai';
import { ai } from './genkit.config';
const result = await ai.generate({
model: openAI.model('gpt-4o'),
prompt: [{ media: { url: imageDataUrl } }],
config: {
visualDetailLevel: 'high', // This causes the error
},
});
Root Cause
In @genkit-ai/compat-oai/lib/model.mjs, the toOpenAIRequestBody() function destructures known config properties but doesn't include visualDetailLevel:
const {
temperature,
maxOutputTokens,
topK,
topP: top_p,
frequencyPenalty: frequency_penalty,
// ... other properties
...restOfConfig // ← visualDetailLevel ends up here!
} = request.config ?? {};
// Later:
body = { ...body, ...restOfConfig }; // ← Spreads visualDetailLevel into API body
Suggested Fix
Add visualDetailLevel to the destructured properties:
const {
temperature,
maxOutputTokens,
topK,
topP: top_p,
frequencyPenalty: frequency_penalty,
logProbs: logprobs,
presencePenalty: presence_penalty,
topLogProbs: top_logprobs,
stopSequences: stop,
version: modelVersion,
tools: toolsFromConfig,
apiKey,
visualDetailLevel, // ← Add this to exclude from restOfConfig
...restOfConfig
} = request.config ?? {};
Environment
- OSX
- @genkit-ai/compat-oai: 1.28.0
- genkit: 1.28.0
Reproduced on current main — visualDetailLevel is still not stripped in toOpenAIRequestBody(), so it falls into restOfConfig and gets spread into the OpenAI request body (causing 400 Unknown parameter: 'visualDetailLevel').
No open PR for this right now (earlier attempts were closed unmerged). I'll open a fix with a regression test unless someone is already working on it.
@genkit-bot triage
Root cause: In js/plugins/compat-oai/src/model.ts, toOpenAIRequestBody passes request.config?.visualDetailLevel to toOpenAIMessages to populate image_url.detail, but omits visualDetailLevel when destructuring request.config ?? {}, causing it to remain in restOfConfig and get spread into the top-level OpenAI request body.
Plan:
- Destructure
visualDetailLevelfromrequest.config ?? {}intoOpenAIRequestBody(js/plugins/compat-oai/src/model.ts) alongsideapiKeyandstrictso it is excluded fromrestOfConfig. - Add a unit test in
js/plugins/compat-oai/tests/compat_oai_test.tscallingtoOpenAIRequestBodywith a media part andconfig: { visualDetailLevel: 'high' }, which reproducesvisualDetailLevelleaking onto the returned body before the fix and verifies thatimage_url.detailis set to'high'without a top-levelvisualDetailLevelfield after (pnpm --dir js/plugins/compat-oai test).
Details
js/plugins/compat-oai/src/model.tslines 581-624:toOpenAIRequestBodyforwardsrequest.config?.visualDetailLeveltotoOpenAIMessages(request.messages, request.config?.visualDetailLevel), then destructures known config keys before mergingrestOfConfigintobody(body = { ...body, ...restOfConfig }).js/plugins/compat-oai/tests/compat_oai_test.ts:describe('toOpenAiRequestBody')tests request body construction directly without network access.
Opened #7244 (Cloud Run logs)