Skip to content

Commit 197c82a

Browse files
committed
fix(openai-chat): gate the chat service tier behind a provider opt-in
Follow-up on top of @Yuxin-Qiao's forwarding commit. The forwarding itself is right: the Responses parser records a caller-supplied service_tier and this adapter dropped it, so the value never reached the wire. But 66 registry providers share the openai-chat adapter, and applyServiceTierGate only strips for openai-responses, so forwarding unconditionally would put an OpenAI-specific field on every one of them. Strict gateways reject unknown body fields, which turns a caller-supplied tier into an upstream 400 on routes that work today. prompt_cache_key directly below already solves this exact problem with an opt-in flag, and its comment names the same hazard. service_tier now follows that precedent: chatServiceTier on the registry entry, threaded through derive and router the way promptCacheKey is. supportsServiceTier is deliberately not reused — it governs the Responses wire through applyServiceTierGate and means something different. No registry entry opts in yet; that wants per-provider evidence that the gateway documents the parameter, which is a separate change.
1 parent f4ea121 commit 197c82a

6 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,15 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
747747
// Preserve a caller-selected service tier for OpenAI-compatible chat gateways. The
748748
// request pipeline deliberately does not inject fast mode for this adapter, but dropping
749749
// an explicit value here makes the Responses parser's serviceTier projection ineffective.
750-
if (parsed.options.serviceTier !== undefined) body.service_tier = parsed.options.serviceTier;
750+
//
751+
// Opt-in, like `prompt_cache_key` directly below: `service_tier` is an OpenAI-specific
752+
// extension and 66 registry providers share this adapter, several of which reject
753+
// unknown body fields. Forwarding unconditionally would turn a caller-supplied
754+
// `service_tier` into an upstream 400 on those routes. `supportsServiceTier` is the
755+
// Responses-wire flag (applyServiceTierGate) and deliberately does not gate this path.
756+
if (provider.chatServiceTier && parsed.options.serviceTier !== undefined) {
757+
body.service_tier = parsed.options.serviceTier;
758+
}
751759
if (modelInList(provider.reasoningSplitModels, parsed.modelId)) body.reasoning_split = true;
752760
const maxTokens = resolveMaxTokens(provider, parsed);
753761
const openRouterRouting = resolveOpenRouterRouting(provider, parsed.modelId);

‎src/providers/derive.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export function providerConfigSeed(entry: ProviderRegistryEntry): OcxProviderCon
239239
...(entry.noPenaltyModels ? { noPenaltyModels: [...entry.noPenaltyModels] } : {}),
240240
...(entry.parallelToolCalls !== undefined ? { parallelToolCalls: entry.parallelToolCalls } : {}),
241241
...(entry.promptCacheKey !== undefined ? { promptCacheKey: entry.promptCacheKey } : {}),
242+
...(entry.chatServiceTier !== undefined ? { chatServiceTier: entry.chatServiceTier } : {}),
242243
...(entry.responsesPath !== undefined ? { responsesPath: entry.responsesPath } : {}),
243244
...(entry.statelessResponses !== undefined ? { statelessResponses: entry.statelessResponses } : {}),
244245
...(entry.requiresAdjacentResponsesToolResults !== undefined
@@ -419,6 +420,7 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig
419420
if (!prov.noPenaltyModels && seed.noPenaltyModels) prov.noPenaltyModels = [...seed.noPenaltyModels];
420421
if (prov.parallelToolCalls === undefined && seed.parallelToolCalls !== undefined) prov.parallelToolCalls = seed.parallelToolCalls;
421422
if (prov.promptCacheKey === undefined && seed.promptCacheKey !== undefined) prov.promptCacheKey = seed.promptCacheKey;
423+
if (prov.chatServiceTier === undefined && seed.chatServiceTier !== undefined) prov.chatServiceTier = seed.chatServiceTier;
422424
// Fill-only: a hand-edited path must survive, and a config saved before the registry
423425
// learned this route still gets backfilled.
424426
if (prov.responsesPath === undefined && seed.responsesPath !== undefined) prov.responsesPath = seed.responsesPath;

‎src/providers/registry.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ export interface ProviderRegistryEntry {
237237
parallelToolCalls?: boolean;
238238
/** Opt this provider into forwarding prompt_cache_key (OpenAI-specific; strict backends reject it). */
239239
promptCacheKey?: boolean;
240+
/**
241+
* Opt-in: forward `service_tier` on the `/chat/completions` wire. Same hazard as
242+
* `promptCacheKey` — an OpenAI-specific extension that strict gateways reject. Distinct from
243+
* `supportsServiceTier`, which governs the Responses wire.
244+
*/
245+
chatServiceTier?: boolean;
240246
autoToolChoiceOnlyModels?: string[];
241247
preserveReasoningContentModels?: string[];
242248
requiresReasoningPlaceholderModels?: string[];

‎src/router.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export function routedProviderConfig(providerName: string, provider: OcxProvider
350350
// opt-in, while an explicit user `false` keeps overriding registry `true`.
351351
...(provider.parallelToolCalls === undefined && registryEntry.parallelToolCalls !== undefined ? { parallelToolCalls: registryEntry.parallelToolCalls } : {}),
352352
...(provider.promptCacheKey === undefined && registryEntry.promptCacheKey !== undefined ? { promptCacheKey: registryEntry.promptCacheKey } : {}),
353+
...(provider.chatServiceTier === undefined && registryEntry.chatServiceTier !== undefined ? { chatServiceTier: registryEntry.chatServiceTier } : {}),
353354
...(provider.reasoningWireFormat === undefined && registryEntry.reasoningWireFormat !== undefined
354355
? { reasoningWireFormat: registryEntry.reasoningWireFormat }
355356
: {}),

‎src/types.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,15 @@ export interface OcxProviderConfig {
14381438
* fields. Default off; only enable for providers that document this parameter.
14391439
*/
14401440
promptCacheKey?: boolean;
1441+
/**
1442+
* Opt-in: forward `service_tier` to the upstream `/chat/completions` body.
1443+
* OpenAI-specific extension with the same hazard as `promptCacheKey` — strict backends
1444+
* reject unknown fields, and 66 registry providers share the `openai-chat` adapter, so a
1445+
* caller-supplied `service_tier` would otherwise turn working requests into upstream 400s.
1446+
* `supportsServiceTier` is the Responses-wire flag and does not apply here.
1447+
* Default off; only enable for providers that document this parameter on the chat wire.
1448+
*/
1449+
chatServiceTier?: boolean;
14411450
/**
14421451
* Provider-local passthrough SSE repair for broken openai-responses gateways that reuse exact
14431452
* placeholder message/reasoning ids or omit the terminal id after a stable added event.

‎tests/openai-chat-hardening.test.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ describe("openai-chat credential hardening", () => {
302302
expect(body).not.toHaveProperty("prompt_cache_key");
303303
});
304304

305-
test("preserves a caller-supplied service tier for the outbound chat body", () => {
306-
const adapter = createOpenAIChatAdapter(provider());
305+
test("preserves a caller-supplied service tier when the provider opts in", () => {
306+
const adapter = createOpenAIChatAdapter(provider({ chatServiceTier: true }));
307307
const req = parsed();
308308
req.options.serviceTier = "priority";
309309

@@ -312,6 +312,27 @@ describe("openai-chat credential hardening", () => {
312312
expect(body.service_tier).toBe("priority");
313313
});
314314

315+
// `service_tier` is an OpenAI-specific extension and this adapter serves 66 registry
316+
// providers, several of which reject unknown body fields. Forwarding it by default would
317+
// turn a caller-supplied tier into an upstream 400 on those routes, so absence of the
318+
// opt-in must mean the field is dropped — the same contract `prompt_cache_key` uses.
319+
test("drops a caller-supplied service tier when the provider has not opted in", () => {
320+
for (const p of [provider(), provider({ chatServiceTier: false })]) {
321+
const req = parsed();
322+
req.options.serviceTier = "priority";
323+
324+
const body = JSON.parse(createOpenAIChatAdapter(p).buildRequest(req).body);
325+
326+
expect(body).not.toHaveProperty("service_tier");
327+
}
328+
});
329+
330+
test("an opted-in provider without a caller tier still sends no service_tier", () => {
331+
const body = JSON.parse(createOpenAIChatAdapter(provider({ chatServiceTier: true })).buildRequest(parsed()).body);
332+
333+
expect(body).not.toHaveProperty("service_tier");
334+
});
335+
315336
test("canonical Kimi Coding Plan routes forward Codex prompt_cache_key", () => {
316337
for (const [providerName, authMode] of [
317338
["kimi", "oauth"],

0 commit comments

Comments
 (0)