Skip to content

fix(ai): send unknown native options as written - #68599

Open
rekram1-node wants to merge 2 commits into
devfrom
native-pass-through
Open

rekram1-node wants to merge 2 commits into
devfrom
native-pass-through

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Text requests now send native keys the SDK doesn't know, unrenamed. Before this, every language protocol silently dropped them, so ai.text({ model, prompt, native: { temperature_k: 1 } }) sent nothing for temperature_k. Image, speech, video, transcription, embedding and decision requests already passed unknown keys through and are unchanged. The rule is in packages/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 }:

-{"model":"gpt-4o","messages":[…],"stream":true,"stream_options":{"include_usage":true},"store":false}
+{"temperature_k":1,"model":"gpt-4o","messages":[…],"stream":true,"stream_options":{"include_usage":true},"store":false}

How it works. The merge happens in one place, Protocol.withNative (packages/ai/src/route/protocol.ts). compile and the Responses trigger-compaction body call it after body.from:

extra = native keys not in protocol.nativeKeys
body  = merge(extra, body)               // mapped body wins
        or, with protocol.nativeField, merge(extra, body[nativeField])
then http.body overlays as before

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 without nativeKeys, 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" } sends reasoning_effort: "high". A body field left undefined doesn't block a raw key of the same name. Nested raw keys merge into mapped objects, so Gemini native: { generationConfig: { temperature_k: 1 } } lands inside generationConfig.

Placement. Unknown keys go at the top level of the body, with one exception:

  • Bedrock Converse uses 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. Its nativeKeys keep exactly that, and the one-off spread in bodyOptions is gone.

Settings core now consumes or drops (loadCatalogPackage in packages/core/src/model-resolver.ts, native packages only; the AI SDK path is untouched):

Setting Now Why
extraBody merged into body (body wins on conflict) Raw request fields, as aisdk-native.ts already does for the packages it rewrites. This catches packages it doesn't rewrite.
disabled, strictJsonSchema dropped v1 kept these in COMPATIBLE_KEYS because it read them (the SDK's strict-schema switch and model hiding) and never sent them.
includeUsage dropped An AI SDK constructor switch v1 always set for openai-compatible. The native Chat protocol already sends stream_options.include_usage.
name dropped An AI SDK constructor name, which v1 passed to create*({ name, ...options }). Never a request field.
canonical provider only for the *-compatible packages Core added it for every package, but only the compatible packages read it. Others would have sent it.
user kept, sent as user A real OpenAI parameter that v1 also sent.
gatewayId (cloudflare-workers-ai) sent as the cf-aig-gateway-id header Cloudflare routes a Workers AI request through an AI Gateway by that header. The package now reads it as a setting.

Core's own controls (timeout, headerTimeout, chunkTimeout, compaction, cache, transport, encoding, tls) were already removed by Provider.nativeSettings and never reach native. Any other settings key is sent as written. The models docs (Options section) now say so and point to body for raw fields that need a specific place in the body.

How did you verify your code works?

  • packages/ai/test/native-options.test.ts is a single table test with one row per family. It records the first request body through a per-request http.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 the src change, all 9 text rows fail.
  • A new packages/core/test/model-resolver.test.ts case checks the settings handling: extraBody becomes the model's http.body, user and an unknown key are sent, and disabled, strictJsonSchema, includeUsage and name are not. It fails without the core change.
  • I compared a local request snapshot of every provider package path, run on origin/dev and on this branch (not committed). The only body diffs are custom and user in 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 the cf-aig-gateway-id header.
  • Commands run:
    • 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 typecheck and bun 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.
    • Repo root: 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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)

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your description already lists the keys this now puts on the wire. Some of them were never request parameters:

  • v1-migrated openai-compatible disabled, strictJsonSchema and extraBody (sent as a literal extraBody object, which is wrong even for servers that accept extra fields)
  • v1 constructor settings like includeUsage / name
  • gatewayId on cloudflare-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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
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.

1 participant