Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(llm): normalize structured message content from OpenAI-compatible providers (#479)

Samc07101c0035b
diff --git a/src/ai/llm/openai.ts b/src/ai/llm/openai.ts
index 9f51849d..c23840d6 100644
--- a/src/ai/llm/openai.ts
+++ b/src/ai/llm/openai.ts
@@ -219,6 +219,28 @@ export class OpenAIChatModel implements ChatModel {
return this.client.chat.completions.create(attempt);
}
+ /**
+ * Normalize provider message content to the plain text representation
+ * expected by the rest of Graft. Some OpenAI-compatible providers
+ * (observed with GLM-5.3 hosted on Mistral's platform) return content as
+ * structured content parts, e.g. `[{ type: "text", text: "..." }]`,
+ * instead of a plain string — this path in Graft assumed the latter.
+ */
+ private static contentToText(content: unknown): string {
+ if (typeof content === "string") return content;
+ if (content == null) return "";
+ if (Array.isArray(content)) {
+ return content
+ .filter((part) => part?.type === "text" || typeof part === "string")
+ .map((part) => (typeof part === "string" ? part : (part.text ?? "")))
+ .join("");
+ }
+ // An unrecognized shape is a signal worth surfacing loudly rather than
+ // silently coercing to "" or "[object Object]" — that hides a future
+ // provider format change instead of flagging it.
+ throw new Error(`Unsupported message.content type from provider: ${typeof content}`);
+ }
+
private fromResponse(
resp: OpenAI.Chat.Completions.ChatCompletion,
format: "text" | "json" | "tool",
@@ -237,7 +259,7 @@ export class OpenAIChatModel implements ChatModel {
}
};
- let text = msg?.content ?? "";
+ let text = OpenAIChatModel.contentToText(msg?.content);
let toolCalls: ToolCall[] = rawCalls.map((c) => ({
id: c.id,
name: c.function.name,
@@ -258,7 +280,7 @@ export class OpenAIChatModel implements ChatModel {
stopReason: choice?.finish_reason ?? null,
assistant: {
role: "assistant",
- content: msg?.content ?? "",
+ content: OpenAIChatModel.contentToText(msg?.content),
toolCalls: toolCalls.length ? toolCalls : undefined,
providerRaw: { provider: PROVIDER, raw: msg },
},
diff --git a/test/llm-adapters.test.ts b/test/llm-adapters.test.ts
index cc11de6a..3b7a4a51 100644
--- a/test/llm-adapters.test.ts
+++ b/test/llm-adapters.test.ts
@@ -179,6 +179,59 @@ test("openai: does NOT paper over a rejected object tool_choice when multiple to
assert.equal(callCount, 1); // no ambiguous retry — the caller asked for "a" specifically
});
+test("openai: array-of-parts message.content is concatenated to plain text", async () => {
+ const { client } = fakeOpenAI(
+ openAiResp({
+ choices: [
+ {
+ message: { content: [{ type: "text", text: "hello " }, { type: "text", text: "there" }], tool_calls: [] },
+ finish_reason: "stop",
+ },
+ ],
+ }),
+ );
+ const m = new OpenAIChatModel({ apiKey: "x", model: "gpt-x", client });
+ const res = await m.create({ messages: [{ role: "user", content: "hi" }] });
+ assert.equal(res.text, "hello there");
+ assert.equal(res.assistant.content, "hello there");
+});
+
+test("openai: non-text parts in an array content are dropped, not stringified", async () => {
+ const { client } = fakeOpenAI(
+ openAiResp({
+ choices: [
+ {
+ message: {
+ content: [{ type: "image_url", image_url: { url: "x" } }, { type: "text", text: "caption" }],
+ tool_calls: [],
+ },
+ finish_reason: "stop",
+ },
+ ],
+ }),
+ );
+ const m = new OpenAIChatModel({ apiKey: "x", model: "gpt-x", client });
+ const res = await m.create({ messages: [{ role: "user", content: "hi" }] });
+ assert.equal(res.text, "caption");
+});
+
+test("openai: null message.content becomes an empty string", async () => {
+ const { client } = fakeOpenAI(
+ openAiResp({ choices: [{ message: { content: null, tool_calls: [] }, finish_reason: "stop" }] }),
+ );
+ const m = new OpenAIChatModel({ apiKey: "x", model: "gpt-x", client });
+ const res = await m.create({ messages: [{ role: "user", content: "hi" }] });
+ assert.equal(res.text, "");
+});
+
+test("openai: an unrecognized message.content shape throws instead of silently stringifying", async () => {
+ const { client } = fakeOpenAI(
+ openAiResp({ choices: [{ message: { content: { weird: true }, tool_calls: [] }, finish_reason: "stop" }] }),
+ );
+ const m = new OpenAIChatModel({ apiKey: "x", model: "gpt-x", client });
+ await assert.rejects(() => m.create({ messages: [{ role: "user", content: "hi" }] }), /Unsupported message\.content type/);
+});
+
// --- Anthropic adapter ------------------------------------------------------
function fakeAnthropic(resp: unknown) {