genkit icon indicating copy to clipboard operation
genkit copied to clipboard

[JS] [compat-oai] visualDetailLevel leaks into OpenAI API request body causing "Unknown parameter" error

Open TAxdorff opened this issue 9 months ago • 4 comments

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

TAxdorff avatar Feb 11 '26 21:02 TAxdorff

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.

usmamalik345 avatar Sep 07 '26 10:09 usmamalik345

@genkit-bot triage

ssbushi avatar Nov 17 '26 18:11 ssbushi

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 visualDetailLevel from request.config ?? {} in toOpenAIRequestBody (js/plugins/compat-oai/src/model.ts) alongside apiKey and strict so it is excluded from restOfConfig.
  • Add a unit test in js/plugins/compat-oai/tests/compat_oai_test.ts calling toOpenAIRequestBody with a media part and config: { visualDetailLevel: 'high' }, which reproduces visualDetailLevel leaking onto the returned body before the fix and verifies that image_url.detail is set to 'high' without a top-level visualDetailLevel field after (pnpm --dir js/plugins/compat-oai test).
Details
  • js/plugins/compat-oai/src/model.ts lines 581-624: toOpenAIRequestBody forwards request.config?.visualDetailLevel to toOpenAIMessages(request.messages, request.config?.visualDetailLevel), then destructures known config keys before merging restOfConfig into body (body = { ...body, ...restOfConfig }).
  • js/plugins/compat-oai/tests/compat_oai_test.ts: describe('toOpenAiRequestBody') tests request body construction directly without network access.

Cloud Run logs

genkit-bot avatar Nov 17 '26 18:11 genkit-bot

Opened #7244 (Cloud Run logs)

genkit-bot avatar Nov 17 '26 19:11 genkit-bot