Repository navigation
fix(ai): send unknown native options as written - #68599
rekram1-node wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
The fix works. I copied the new table test onto the base branch and all 9 text-protocol rows fail there; with this PR they pass, along with the changed Anthropic and tool-updates tests. typecheck:tsc for packages/ai is clean. Putting this in Protocol.withNative, with the mapped body winning on collisions, keeps it in one place.
Public API: the ai package's Protocol interface gets two optional fields, nativeKeys and nativeField, and gatewayProtocol now requires nativeKeys. Behavior change: unmapped native keys, including unconsumed opencode.json provider/model options, are now sent in text request bodies. Config keys and the schema are unchanged.
One concern, inline: v1-migrated settings that never reached the API before are now sent, and some APIs reject unknown fields.
| body: Body, | ||
| ): Body => { | ||
| const known = protocol.nativeKeys ?? [] | ||
| const extra = Object.fromEntries(Object.entries(native ?? {}).filter(([key]) => !known.includes(key))) |
There was a problem hiding this comment.
Your description already lists the keys this now puts on the wire. Some of them were never request parameters:
- v1-migrated openai-compatible
disabled,strictJsonSchemaandextraBody(sent as a literalextraBodyobject, which is wrong even for servers that accept extra fields) - v1 constructor settings like
includeUsage/name gatewayIdoncloudflare-workers-ai
Some APIs reject unknown fields instead of ignoring them. OpenAI Chat, for example, returns 400 Unrecognized request argument supplied. A migrated config that used to work could start failing on every request with no change on the user's side. Could core drop these known non-request settings before they become native (e.g. the COMPATIBLE_KEYS settings that aren't mapped, plus v1 constructor-only options)? And could extraBody be merged into the body instead of being sent literally? That would keep this change to genuinely unknown request parameters.
There was a problem hiding this comment.
This is intentional. The owner asked for configs to send keys that used to be dropped, and for core not to strip them. That's why the description lists them instead of filtering them out. Dropping the v1 COMPATIBLE_KEYS leftovers or merging extraBody would change how core migrates config, so it belongs in its own follow-up if the owner wants it. It shouldn't be folded in here.
There was a problem hiding this comment.
Understood. Your description already lists the keys that will now reach the wire, so the behaviour change is documented, and I'm treating this thread as settled for this PR.
One thing should still go in the follow-up or the release note: strict APIs reject unknown fields. OpenAI Chat, for example, returns 400 Unrecognized request argument supplied. A user with a V1-migrated openai-compatible config that still has disabled, strictJsonSchema or extraBody can start getting that error on every request after upgrading, without changing anything. The fix on their side is to remove those keys from opencode.json, or to move the extraBody contents into the request body. That fix will be easier to find if the release note says so.
I didn't re-run anything or change anything in repo/ for this reply.
03c34e5 to
51ebc5b
Compare
There was a problem hiding this comment.
The new commit addresses the earlier point about settings that were never request parameters: core now drops the v1 switches, merges extraBody into body, sends gatewayId as the cf-aig-gateway-id header, and passes provider only to the *-compatible packages. I checked that the new model-resolver test fails with the previous commit's resolver and passes now. The packages/ai tests and typecheck:tsc also pass.
Public API: Protocol gets optional nativeKeys/nativeField, gatewayProtocol now requires nativeKeys, and cloudflareWorkersAI gets an optional gatewayId setting. Config keys are unchanged, but unrecognized provider/model settings are now sent in text request bodies.
No new concerns.
Issue for this PR
Closes #
Type of change
What does this PR do?
Text requests now send
nativekeys the SDK doesn't know, unrenamed. Before this, every language protocol silently dropped them, soai.text({ model, prompt, native: { temperature_k: 1 } })sent nothing fortemperature_k. Image, speech, video, transcription, embedding and decision requests already passed unknown keys through and are unchanged. The rule is inpackages/ai/AGENTS.md: send what callers pass, and keep strict parsing for closed types we own.Because config settings now reach the wire, core consumes the settings that were never request parameters before they become
native.Captured Chat Completions body for
native: { temperature_k: 1 }:How it works. The merge happens in one place,
Protocol.withNative(packages/ai/src/route/protocol.ts).compileand the Responses trigger-compaction body call it afterbody.from:Each language protocol lists the keys it maps in
nativeKeys(Object.keys(Options.fields)), and wrappers add their own keys to the inner protocol's. A protocol withoutnativeKeys, such as a custom API whose body is the request itself, sends no extras.Collisions. Unknown keys deep-merge under the mapped body, so mapped values win at every leaf. For example, Chat with native
{ reasoningEffort: "high", reasoning_effort: "low" }sendsreasoning_effort: "high". A body field leftundefineddoesn't block a raw key of the same name. Nested raw keys merge into mapped objects, so Gemininative: { generationConfig: { temperature_k: 1 } }lands insidegenerationConfig.Placement. Unknown keys go at the top level of the body, with one exception:
additionalModelRequestFields, the slot Converse defines for model-specific parameters.Preserved quirk. OpenRouter Chat already passed unknown keys as written, including Chat option names like
reasoningEffort. ItsnativeKeyskeep exactly that, and the one-off spread inbodyOptionsis gone.Settings core now consumes or drops (
loadCatalogPackageinpackages/core/src/model-resolver.ts, native packages only; the AI SDK path is untouched):extraBodybody(body wins on conflict)aisdk-native.tsalready does for the packages it rewrites. This catches packages it doesn't rewrite.disabled,strictJsonSchemaCOMPATIBLE_KEYSbecause it read them (the SDK's strict-schema switch and model hiding) and never sent them.includeUsagestream_options.include_usage.namecreate*({ name, ...options }). Never a request field.provider*-compatiblepackagesuserusergatewayId(cloudflare-workers-ai)cf-aig-gateway-idheaderCore's own controls (
timeout,headerTimeout,chunkTimeout,compaction,cache,transport,encoding,tls) were already removed byProvider.nativeSettingsand never reachnative. Any othersettingskey is sent as written. The models docs (Options section) now say so and point tobodyfor raw fields that need a specific place in the body.How did you verify your code works?
packages/ai/test/native-options.test.tsis a single table test with one row per family. It records the first request body through a per-requesthttp.fetch. The text rows are Responses, Open Responses, Chat (also checks the collision rule), Anthropic, Gemini generate-content and Interactions, Bedrock Converse, Cohere and Mistral. The other rows are image, speech, transcription, video, embedding and decision. Without thesrcchange, all 9 text rows fail.packages/core/test/model-resolver.test.tscase checks the settings handling:extraBodybecomes the model'shttp.body,userand an unknown key are sent, anddisabled,strictJsonSchema,includeUsageandnameare not. It fails without the core change.origin/devand on this branch (not committed). The only body diffs arecustomanduserin the flat-options variants (openai, openai/chat, anthropic, deepseek, openai-compatible), both genuinely unknown native keys. cloudflare-workers-ai's body is unchanged, and it gains only thecf-aig-gateway-idheader.packages/ai:bun typecheck,bun run typecheck:tsc,bun test. The only failures are the expected diffs in the uncommitted local snapshot.packages/core:bun typecheckandbun test. 5 failures; I didn't investigate them and none touch provider requests: one test-environment isolation test and four shell-tool tests.packages/plugin:bun typecheck.bun run check.Net lines:
src+88/−19 (ai and core); +220/−21 in total, including the 98-line table test, the 29-line core test and one line of docs.Checklist
Added by the opencode bot (AI-generated).
Public API: ai Protocol gains optional nativeKeys/nativeField, gatewayProtocol requires nativeKeys, cloudflareWorkersAI takes gatewayId; unrecognized native/config options are now sent in text request bodies (behavior change)