diff --git a/packages/ai/src/protocols/openai-chat.ts b/packages/ai/src/protocols/openai-chat.ts index da0622e226f..ee1c5626b76 100644 --- a/packages/ai/src/protocols/openai-chat.ts +++ b/packages/ai/src/protocols/openai-chat.ts @@ -577,6 +577,9 @@ const lowerAssistantMessage = Effect.fnUntraced(function* ( const opaque = reasoningOpaque(reasoning, options.providerMetadataKey) const reasoningText = reasoning.length === 0 || (opaque !== undefined && text === "") ? (requireReasoning ? "" : undefined) : text + // A model that declares a different reasoning field reads reasoning there, so structured details and opaque values + // from earlier turns (including another model served under the same provider metadata key) must not be replayed. + const replaysStructured = configuredField === undefined || configuredField === "reasoning_details" const result = { role: "assistant" as const, content: (() => { @@ -585,10 +588,11 @@ const lowerAssistantMessage = Effect.fnUntraced(function* ( return content.map((part) => part.text).join("") })(), ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}), - ...(details !== undefined ? { reasoning_details: details } : {}), - ...(opaque !== undefined ? { reasoning_opaque: opaque } : {}), + ...(details !== undefined && replaysStructured ? { reasoning_details: details } : {}), + ...(opaque !== undefined && replaysStructured ? { reasoning_opaque: opaque } : {}), } - if (field === undefined || reasoningText === undefined) return result + // `reasoning_details` is the structured list itself, so scalar text must not be written over it. + if (field === undefined || reasoningText === undefined || field === "reasoning_details") return result return { ...result, [field]: reasoningText } }) diff --git a/packages/ai/test/provider/openai-chat-reasoning.recorded.test.ts b/packages/ai/test/provider/openai-chat-reasoning.recorded.test.ts index 3f2e7c28c08..c5dd2d20c1c 100644 --- a/packages/ai/test/provider/openai-chat-reasoning.recorded.test.ts +++ b/packages/ai/test/provider/openai-chat-reasoning.recorded.test.ts @@ -1,6 +1,6 @@ import { describe, expect } from "bun:test" import { Effect } from "effect" -import { ai, LanguageModel, LLMEvent, LLMResponse } from "../../src/effect.js" +import { ai, LLMEvent, LLMResponse } from "../../src/effect.js" import * as OpenAICompatible from "../../src/providers/openai-compatible.js" import * as OpenRouter from "../../src/providers/openrouter.js" import { LLMClient } from "../../src/route.js" @@ -11,28 +11,22 @@ import { expectWeatherToolLoop, goldenWeatherToolLoopRequest, runWeatherToolLoop const cases = [ { name: "OpenRouter", - model: LanguageModel.update( - OpenRouter.configure({ - apiKey: process.env.OPENROUTER_API_KEY ?? "fixture", - providerOptions: { reasoning: { max_tokens: 1024 } }, - }).model("anthropic/claude-sonnet-4.6"), - { compatibility: { reasoningField: "reasoning" } }, - ), + model: OpenRouter.configure({ + apiKey: process.env.OPENROUTER_API_KEY ?? "fixture", + providerOptions: { reasoning: { max_tokens: 1024 } }, + }).model("anthropic/claude-sonnet-4.6"), requires: ["OPENROUTER_API_KEY"], cassette: "openrouter-reasoning", structured: true, }, { name: "Vercel AI Gateway", - model: LanguageModel.update( - OpenAICompatible.configure({ - provider: "vercel-ai-gateway", - baseURL: "https://ai-gateway.vercel.sh/v1", - apiKey: process.env.AI_GATEWAY_API_KEY ?? "fixture", - http: { body: { reasoning: { enabled: true, max_tokens: 1024 } } }, - }).model("anthropic/claude-sonnet-4.6"), - { compatibility: { reasoningField: "reasoning" } }, - ), + model: OpenAICompatible.configure({ + provider: "vercel-ai-gateway", + baseURL: "https://ai-gateway.vercel.sh/v1", + apiKey: process.env.AI_GATEWAY_API_KEY ?? "fixture", + http: { body: { reasoning: { enabled: true, max_tokens: 1024 } } }, + }).model("anthropic/claude-sonnet-4.6"), requires: ["AI_GATEWAY_API_KEY"], cassette: "vercel-ai-gateway-reasoning", structured: true, diff --git a/packages/ai/test/provider/openai-chat.test.ts b/packages/ai/test/provider/openai-chat.test.ts index c3cadaed4e0..0c194743bf6 100644 --- a/packages/ai/test/provider/openai-chat.test.ts +++ b/packages/ai/test/provider/openai-chat.test.ts @@ -1247,6 +1247,85 @@ describe("OpenAI Chat route", () => { }), ) + // A mid-session model switch can keep the same provider metadata key while the new model reads reasoning from a + // declared field of its own; details replayed from the earlier model are refused by some endpoints. + it.effect("does not replay reasoning details when the model declares another reasoning field", () => + Effect.gen(function* () { + const details = [ + { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 }, + ] + const replay = yield* compileRequest( + ai.request({ + model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_content" } }), + messages: [ + Message.assistant([ + { + type: "reasoning", + text: "thinking", + providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } }, + }, + { type: "text", text: "Hello" }, + ]), + ], + }), + ) + + expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }]) + }), + ) + + it.effect("keeps replayed reasoning details when the model declares reasoning_details", () => + Effect.gen(function* () { + const details = [ + { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 }, + ] + const replay = yield* compileRequest( + ai.request({ + model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_details" } }), + messages: [ + Message.assistant([ + { + type: "reasoning", + text: "thinking", + providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } }, + }, + { type: "text", text: "Hello" }, + ]), + ], + }), + ) + + expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: details }]) + }), + ) + + it.effect("keeps replayed reasoning details when the model declares no reasoning field", () => + Effect.gen(function* () { + const details = [ + { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 }, + ] + const replay = yield* compileRequest( + ai.request({ + model, + messages: [ + Message.assistant([ + { + type: "reasoning", + text: "thinking", + providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } }, + }, + { type: "text", text: "Hello" }, + ]), + ], + }), + ) + + expect(replay.body.messages).toEqual([ + { role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: details }, + ]) + }), + ) + it.effect("reports the cost the endpoint billed", () => Effect.gen(function* () { const response = yield* LLMClient.generate(request).pipe(