The Open Framework for Building Agents
fix(eve): Fix request format for `experimental_chatgpt` (#974)
Ben Pankow9a1800f37ddd
diff --git a/.changeset/codex-hoist-instructions.md b/.changeset/codex-hoist-instructions.mdnew file mode 100644index 000000000..0bd5fe2b7--- /dev/null+++ b/.changeset/codex-hoist-instructions.md@@ -0,0 +1,8 @@+---+"eve": patch+---++Fix `experimental_chatgpt` sending requests in an improper format that the+Codex backend rejected with a 400 Bad Request: system instructions are now sent+in the top-level `instructions` field and the unsupported `max_output_tokens`+parameter is dropped.diff --git a/packages/eve/src/public/models/openai/chatgpt/model.test.ts b/packages/eve/src/public/models/openai/chatgpt/model.test.tsindex 9df8076ab..63c2627c4 100644--- a/packages/eve/src/public/models/openai/chatgpt/model.test.ts+++ b/packages/eve/src/public/models/openai/chatgpt/model.test.ts@@ -88,6 +88,55 @@ describe("Codex model", () => {"msg_070f78d118bbc2a4016a4565689d4c8190b455e3c0b74eaf90",);});++ it("shapes the request the way the Codex backend accepts", async () => {+ const requests: RecordedRequest[] = [];+ const model = createCodexSubscriptionModel(+ { model: "gpt-5.2-codex" },+ {+ codexApiEndpoint: CODEX_ENDPOINT,+ fetch: createRecordingFetch(requests),+ readCredentials: async () => ({+ kind: "chatgpt",+ accessToken: createUnsignedJwt({ exp: 2_000_000_000 }),+ authPath: "/home/user/.codex/auth.json",+ codexHome: "/home/user/.codex",+ }),+ },+ );++ await model.doGenerate({+ prompt: [+ { role: "system", content: "You are a helpful assistant." },+ { role: "user", content: [{ type: "text", text: "hello" }] },+ ],+ maxOutputTokens: 1024,+ temperature: 0.7,+ topP: 0.9,+ providerOptions: { openai: { reasoningEffort: "medium", reasoningSummary: "auto" } },+ });++ expect(requests).toHaveLength(1);+ const body = JSON.parse(requests[0]?.body ?? "{}");+ // The Codex backend requires instructions at the top level and rejects a+ // `developer`/`system` role in the input array.+ expect(body.instructions).toBe("You are a helpful assistant.");+ expect(body.input).toEqual([+ { role: "user", content: [{ type: "input_text", text: "hello" }] },+ ]);+ // The system prompt is hoisted, not duplicated into the input array.+ expect(body.input).not.toContainEqual(expect.objectContaining({ role: "system" }));+ expect(body.input).not.toContainEqual(expect.objectContaining({ role: "developer" }));+ // The Codex backend rejects response storage and `max_output_tokens`, and+ // never accepts sampling parameters for reasoning models.+ expect(body.store).toBe(false);+ expect(body.max_output_tokens).toBeUndefined();+ expect(body.temperature).toBeUndefined();+ expect(body.top_p).toBeUndefined();+ // Stateless reasoning requires the encrypted reasoning payload to be+ // echoed back, so `include` must carry it whenever reasoning is set.+ expect(body.include).toContain("reasoning.encrypted_content");+ });});interface RecordedRequest {diff --git a/packages/eve/src/public/models/openai/chatgpt/model.ts b/packages/eve/src/public/models/openai/chatgpt/model.tsindex fd3f85bf1..e23669bfd 100644--- a/packages/eve/src/public/models/openai/chatgpt/model.ts+++ b/packages/eve/src/public/models/openai/chatgpt/model.ts@@ -52,19 +52,46 @@ function normalizeCodexCallOptions(const providerOptions = options.providerOptions;const openaiOptions = providerOptions?.openai ?? {};+ // The Codex backend requires system instructions in the top-level+ // `instructions` field and rejects a `developer`/`system` role inside the+ // `input` array (the shape the AI SDK produces by default). Hoist the system+ // messages out of the prompt and into `instructions` before delegation.+ const { instructions, prompt } = hoistSystemInstructions(options.prompt);++ // The Codex backend rejects `max_output_tokens` with+ // `400 Unsupported parameter`, so drop it before delegation.+ const { maxOutputTokens: _maxOutputTokens, ...rest } = options;+return {- ...options,- prompt: stripOpenAIItemIdsFromPrompt(options.prompt),+ ...rest,+ prompt: stripOpenAIItemIdsFromPrompt(prompt),providerOptions: {...providerOptions,openai: {...openaiOptions,+ ...(instructions !== undefined && { instructions }),store: false,},},};}+function hoistSystemInstructions(prompt: LanguageModelV4CallOptions["prompt"]): {+ readonly instructions: string | undefined;+ readonly prompt: LanguageModelV4CallOptions["prompt"];+} {+ const systemContent = prompt+ .filter((message) => message.role === "system")+ .map((message) => message.content);+ if (systemContent.length === 0) {+ return { instructions: undefined, prompt };+ }+ return {+ instructions: systemContent.join("\n\n"),+ prompt: prompt.filter((message) => message.role !== "system"),+ };+}+function stripOpenAIItemIdsFromPrompt(prompt: LanguageModelV4CallOptions["prompt"],): LanguageModelV4CallOptions["prompt"] {