fix(sdk): stop JSON-wrapping structured tool_result content in the Anthropic adapter (#3797)

## Description

`withHeadroom()` for the Anthropic SDK
(`sdk/typescript/src/adapters/anthropic.ts`) carried its own private
copies of the Anthropic ⇄ OpenAI message converters instead of using the
exported ones in `sdk/typescript/src/utils/format.ts`. The two copies
had drifted. For a `tool_result` block whose `content` is an **array of
blocks** (the documented Anthropic form, and the required one whenever a
tool returns more than a plain string), the adapter's copy serialised
the whole array with `JSON.stringify`, while the shared converter
flattens it to newline-joined text.

So a wrapped `messages.create()` sent the proxy a tool message whose
content was `[{"type":"text","text":"row 1"},{"type":"text","text":"row
2"}]` instead of `row 1\nrow 2`. The compressor then works on a
JSON-escaped blob rather than the tool's actual output (defeating the
content detectors for logs, tables, etc.), and after compression the
model receives the JSON-wrapped text as the tool result.

This removes the private copies and imports the shared converters, so
the adapter behaves exactly like the universal `compress()` path does
for Anthropic input. Net effect: −179 lines in the adapter, one import
added; the wrapper's flow (convert → compress → convert back →
`messages.create`) is unchanged. This is the de-duplication left as a
follow-up in #3439.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- **`sdk/typescript/src/adapters/anthropic.ts`** — delete the private
`anthropicImageUrl`, `openAIImageBlock`, `anthropicToOpenAI` and
`openAIToAnthropic`; import `anthropicToOpenAI` / `openAIToAnthropic`
from `../utils/format.js`; drop the now-unused `OpenAIMessage`,
`AssistantMessage`, `ToolCall` type imports. `withHeadroom` itself is
untouched (file goes from 252 to 73 lines).
- **`sdk/typescript/test/adapters/anthropic.test.ts`** — new test: an
array-content `tool_result` reaches `/v1/compress` as `{ role: "tool",
content: "row 1\nrow 2", tool_call_id }`. Fails on `main`.
- **`sdk/typescript/test/utils/format.test.ts`** — characterisation test
pinning the shared `anthropicToOpenAI` flattening that the adapter now
relies on (passes on `main`; that branch of the shared converter was
previously untested).

Behaviour differences on the adapter path, both bringing it in line with
the shared converter:
1. Array-content `tool_result` → newline-joined text instead of a JSON
string (**the fix**).
2. A `tool_use.input` given as a string is passed through as the OpenAI
`arguments` instead of being re-`JSON.stringify`ed. Anthropic defines
`input` as an object, so this cannot affect a valid request.

Everything else (string and text-only turns, images including the #3439
round trip, object `tool_use` inputs, system/tool role mapping) is
byte-identical between the two copies, so no other behaviour changes.

## Testing

- [x] Unit tests pass (`npm test` — vitest, incl. typecheck)
- [x] Type checking passes (`npm run typecheck` — tsc --noEmit)
- [x] Build passes (`npm run build` — tsup)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

```text
$ cd sdk/typescript && npx vitest run test/adapters/anthropic.test.ts
 Test Files  1 passed (1)
      Tests  10 passed (10)

$ npx vitest run test/utils/format.test.ts
 Test Files  1 passed (1)
      Tests  34 passed (34)

$ npm test                       # full suite
 Test Files  1 failed | 18 passed | 5 skipped (24)
      Tests  1 failed | 333 passed | 33 skipped (367)
# The one failure is test/types.test-d.ts:177 and is pre-existing on main: #3424's
# enforced `compress` parameters assertion predates the `config` option #3370 added
# to the interface. Fixed separately in #3796.

$ npm run typecheck              # tsc --noEmit
(clean, no errors)

$ npm run build                  # tsup
CJS / ESM / DTS Build success
```

## Real Behavior Proof

- Environment: macOS (Apple silicon), Node v22.16.0, npm 10.9.2; `cd
sdk/typescript && npm ci`.
- Exact command / steps: `npx vitest run
test/adapters/anthropic.test.ts` with the new test on `main` and on this
branch; then `npm test`, `npm run typecheck`, `npm run build`.
- Observed result: On `main` the new test fails — the `/v1/compress`
request carries the tool message content as
`"[{\"type\":\"text\",\"text\":\"row
1\"},{\"type\":\"text\",\"text\":\"row 2\"}]"` where `"row 1\nrow 2"` is
expected. With the change it passes and the request body carries `{
role: "tool", content: "row 1\nrow 2", tool_call_id: "tu_1" }`. Full
suite 333 passed (the single remaining failure is the pre-existing type
assertion noted above); typecheck and build clean.
- Not tested: a live proxy round trip. The conversion is pure and
covered by the unit tests; the proxy consumes standard OpenAI-format
messages either way.

## Runtime Rollout Safety

- Rollout-managed feature(s): None. Client-side TypeScript SDK adapter
fix; no server/runtime feature flag or rollout channel involved.
- Minimum rollout channel: N/A.
- Stable/default behavior changed: Only the buggy path — array-content
tool results are now sent to the proxy as text instead of a JSON string.
Text-only, image and object-`tool_use` conversions are unchanged.
- Kill switch / disable path: N/A — correctness fix, not a gated
feature.
- Unsafe override required: No.
- Qualification impact: None — the adapter now uses the converter
already exercised by the universal `compress()` tests.
- Rollback path: Revert this PR; no persisted state, migrations, or
config to unwind.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Screenshots (if applicable)

N/A

## Additional Notes

- Pure deletion plus one import; the shared converters gained no new
code and no other adapter is touched.
- Out of scope (pre-existing in both copies): non-text blocks *inside* a
`tool_result` (e.g. images returned by a tool) are JSON-stringified by
the shared converter. A follow-up could map them to image parts the way
#3439 did for user turns.
- CHANGELOG left untouched — the `no-manual-changelog` CI guard owns it;
the Conventional Commit title drives the entry.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
Shubham Srivastava
2026-09-29 16:22:06 -05:00
committed by GitHub
co-authored by Claude Fable 5.1 JD Davis
parent 1854fd7fd3
commit 2e4a60ae37
3 changed files with 69 additions and 181 deletions
+2 -181
View File
@@ -1,6 +1,6 @@
import { compress } from "../compress.js";
import type { CompressOptions, OpenAIMessage } from "../types.js";
import type { AssistantMessage, ToolCall } from "../types.js";
import type { CompressOptions } from "../types.js";
import { anthropicToOpenAI, openAIToAnthropic } from "../utils/format.js";
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -11,185 +11,6 @@ interface AnthropicLike {
[key: string]: any;
}
// Anthropic image block `source` -> OpenAI image_url url string, or null if unconvertible.
function anthropicImageUrl(source: any): string | null {
if (!source || typeof source !== "object") return null;
if (source.type === "base64" && source.media_type && source.data) {
return `data:${source.media_type};base64,${source.data}`;
}
if (source.type === "url" && typeof source.url === "string") return source.url;
return null;
}
// OpenAI image_url url -> Anthropic image block. data: URIs become a base64 source; other urls a url source.
function openAIImageBlock(url: string): any {
const m = /^data:([^;,]+);base64,(.*)$/s.exec(url);
if (m) return { type: "image", source: { type: "base64", media_type: m[1], data: m[2] } };
return { type: "image", source: { type: "url", url } };
}
/**
* Convert Anthropic messages to OpenAI format for compression.
*
* Anthropic format:
* { role: 'user' | 'assistant', content: string | ContentBlock[] }
* ContentBlock = { type: 'text', text } | { type: 'tool_use', ... } | { type: 'tool_result', ... }
*/
function anthropicToOpenAI(messages: any[]): OpenAIMessage[] {
const result: OpenAIMessage[] = [];
for (const msg of messages) {
if (msg.role === "user") {
if (typeof msg.content === "string") {
result.push({ role: "user", content: msg.content });
} else if (Array.isArray(msg.content)) {
const toolResults = msg.content.filter(
(b: any) => b.type === "tool_result",
);
const hasImages = msg.content.some((b: any) => b.type === "image");
if (hasImages) {
const parts = msg.content
.filter((b: any) => b.type === "text" || b.type === "image")
.map((b: any) => {
if (b.type === "text") return { type: "text" as const, text: b.text };
const url = anthropicImageUrl(b.source);
return url ? { type: "image_url" as const, image_url: { url } } : null;
})
.filter((p: any): p is NonNullable<typeof p> => p !== null);
if (parts.length > 0) {
result.push({ role: "user", content: parts });
}
} else {
const textBlocks = msg.content.filter((b: any) => b.type === "text");
if (textBlocks.length > 0) {
result.push({
role: "user",
content: textBlocks.map((b: any) => b.text).join("\n"),
});
}
}
for (const tr of toolResults) {
result.push({
role: "tool",
content:
typeof tr.content === "string"
? tr.content
: JSON.stringify(tr.content),
tool_call_id: tr.tool_use_id,
});
}
}
continue;
}
if (msg.role === "assistant") {
if (typeof msg.content === "string") {
result.push({ role: "assistant", content: msg.content });
} else if (Array.isArray(msg.content)) {
const textBlocks = msg.content.filter((b: any) => b.type === "text");
const toolUseBlocks = msg.content.filter(
(b: any) => b.type === "tool_use",
);
const content =
textBlocks.length > 0
? textBlocks.map((b: any) => b.text).join("\n")
: null;
const openaiMsg: AssistantMessage = { role: "assistant", content };
if (toolUseBlocks.length > 0) {
openaiMsg.tool_calls = toolUseBlocks.map(
(b: any): ToolCall => ({
id: b.id,
type: "function",
function: {
name: b.name,
arguments: JSON.stringify(b.input),
},
}),
);
}
result.push(openaiMsg);
}
continue;
}
}
return result;
}
/**
* Convert compressed OpenAI messages back to Anthropic format.
*/
function openAIToAnthropic(messages: OpenAIMessage[]): any[] {
const result: any[] = [];
for (const msg of messages) {
if (msg.role === "user") {
if (typeof msg.content === "string") {
result.push({ role: "user", content: msg.content });
} else if (Array.isArray(msg.content)) {
result.push({
role: "user",
content: msg.content.map((p) => {
if (p.type === "text") return { type: "text", text: p.text };
if (p.type === "image_url") return openAIImageBlock(p.image_url.url);
return { type: "text", text: "" };
}),
});
}
continue;
}
if (msg.role === "assistant") {
const blocks: any[] = [];
if (msg.content) blocks.push({ type: "text", text: msg.content });
if (msg.tool_calls) {
for (const tc of msg.tool_calls) {
blocks.push({
type: "tool_use",
id: tc.id,
name: tc.function.name,
input: JSON.parse(tc.function.arguments),
});
}
}
result.push({
role: "assistant",
content:
blocks.length === 1 && blocks[0].type === "text"
? blocks[0].text
: blocks,
});
continue;
}
if (msg.role === "tool") {
result.push({
role: "user",
content: [
{
type: "tool_result",
tool_use_id: msg.tool_call_id,
content: msg.content,
},
],
});
continue;
}
if (msg.role === "system") {
// System messages are handled separately in Anthropic (as `system` param)
// Pass through as user message if it appears in the messages array
result.push({ role: "user", content: msg.content });
}
}
return result;
}
/**
* Wrap an Anthropic client to auto-compress messages before each request.
*
@@ -190,6 +190,52 @@ describe("withHeadroom (Anthropic)", () => {
expect(createArgs.messages.length).toBeGreaterThan(0);
});
it("flattens array-content tool_result blocks to text for the compression request", async () => {
mockFetch.mockResolvedValueOnce(mockCompressSuccess());
const mockCreate = vi.fn().mockResolvedValue({});
const fakeClient = { messages: { create: mockCreate } };
const wrapped = withHeadroom(fakeClient as any, {
baseUrl: "http://localhost:8787",
});
await wrapped.messages.create({
model: "claude-sonnet-4-5-20250929",
messages: [
{ role: "user", content: "run the query" },
{
role: "assistant",
content: [
{ type: "tool_use", id: "tu_1", name: "query", input: { sql: "select 1" } },
],
},
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "tu_1",
content: [
{ type: "text", text: "row 1" },
{ type: "text", text: "row 2" },
],
},
],
},
],
max_tokens: 1024,
});
// The proxy must receive the tool output as text, not a JSON-encoded block array.
const body = JSON.parse(mockFetch.mock.calls[0][1].body);
expect(body.messages[2]).toEqual({
role: "tool",
content: "row 1\nrow 2",
tool_call_id: "tu_1",
});
});
it("preserves image blocks instead of dropping them during compression", async () => {
mockFetch.mockResolvedValueOnce(mockCompressSuccess());
+21
View File
@@ -367,6 +367,27 @@ describe("anthropicToOpenAI", () => {
]);
expect(result).toEqual([]);
});
it("flattens array-content tool_result blocks to newline-joined text", () => {
const result = anthropicToOpenAI([
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: "tu_1",
content: [
{ type: "text", text: "row 1" },
{ type: "text", text: "row 2" },
],
},
],
},
]);
expect(result).toEqual([
{ role: "tool", content: "row 1\nrow 2", tool_call_id: "tu_1" },
]);
});
});
describe("openAIToAnthropic", () => {