AI providers: tool results cannot carry images because non-string results are JSON.stringify'd
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- typescript
- 領域
- ai, backend-api-design
調査の方向性
Read packages/effect/src/ai/Toolkit.ts and Prompt.ts, then trace tool result serialization in packages/ai/anthropic/src/AnthropicLanguageModel.ts, packages/ai/openai/src/OpenAiLanguageModel.ts, and packages/ai/openai-compat/src/OpenAiLanguageModel.ts. Compare the Anthropic and OpenAI Responses content formats described in the issue and decide how model-facing content should be represented. Done means supported rich results reach compatible APIs in their native form, with text-only providers retaining current behavior or reporting a clear error.
索引モデルが issue の本文から書いたものです。
説明
What is the problem?
A tool result cannot carry an image (or other content blocks) to the model. The provider packages turn every non-string tool result into a string with JSON.stringify, so the model gets the image as plain text.
Both provider APIs support rich tool results:
- Anthropic Messages:
tool_result.contentcan be a string or an array of content blocks, includingimage(base64 or URL) anddocumentblocks. - OpenAI Responses:
function_call_output.outputcan be a string or an array ofinput_text,input_image, andinput_fileitems.
Where it happens
The path from a tool handler to the request (on main, 4d24a11; the same in 4.0.5 and 4.0.6):
Toolkitencodes the handler result with the tool's success schema. There is no hook to map it to model content:
https://github.com/Effect-TS/effect/blob/4d24a11217edda68e8c4c72cdc1cff025afa19d6/packages/effect/src/ai/Toolkit.ts#L527Prompt.ToolResultPart.resultisunknown, and it getsencodedResult:
https://github.com/Effect-TS/effect/blob/4d24a11217edda68e8c4c72cdc1cff025afa19d6/packages/effect/src/ai/Prompt.ts#L2268- The providers stringify anything that is not a string:
@effect/ai-anthropic: https://github.com/Effect-TS/effect/blob/4d24a11217edda68e8c4c72cdc1cff025afa19d6/packages/ai/anthropic/src/AnthropicLanguageModel.ts#L1045@effect/ai-openai: https://github.com/Effect-TS/effect/blob/4d24a11217edda68e8c4c72cdc1cff025afa19d6/packages/ai/openai/src/OpenAiLanguageModel.ts#L1373@effect/ai-openai-compat: https://github.com/Effect-TS/effect/blob/4d24a11217edda68e8c4c72cdc1cff025afa19d6/packages/ai/openai-compat/src/OpenAiLanguageModel.ts#L923
content: typeof part.result === "string" ? part.result : JSON.stringify(part.result),
Example
A tool (for example, a screenshot tool) returns this success value:
[{ type: "image", source: { type: "base64", media_type: "image/png", data: "iVBORw0..." } }]
@effect/ai-anthropic sends:
{ "type": "tool_result", "tool_use_id": "...", "content": "[{\"type\":\"image\",\"source\":{...}}]" }
The API treats the string as one text block. So:
- The model cannot see the image. It only reads base64 characters.
- The base64 text uses a very large number of tokens. A 500 KB PNG is about 670,000 characters, which can be more than the whole context window. A real image block costs about
width * height / 750tokens on Claude.
The same thing happens with function_call_output.output on OpenAI.
Expected behavior
A tool should be able to return model-facing content parts (text, image, file), and each provider should send them in its native form:
- Anthropic:
tool_result.contentas an array oftext/image/documentblocks. - OpenAI Responses:
function_call_output.outputas an array ofinput_text/input_image/input_file. - Providers or APIs that only accept text tool results (for example, Chat Completions
toolmessages) can keep the current string behavior, or fail with a clear error.
Possible API
Two ideas (I am open to others):
- An optional mapping on the tool, like
toModelOutputin the Vercel AI SDK. It maps the decoded result to content parts that use the existingPromptpart types (TextPart,FilePart). Theresultthat the program sees stays typed as today. - A known shape for
ToolResultPart.result, such as{ type: "content", value: Array<TextPart | FilePart> }, that providers check for before they fall back toJSON.stringify.
Workaround today
Put the image in a separate user message after the tool result. This works, but it moves the image out of the tool call that made it, and it does not fit well with LanguageModel.generateText tool loops.
Versions
The problem is in 4.0.5 and in the latest release, 4.0.6. The reproduction in the comment below gives the same output on both.
effect4.0.5, 4.0.6@effect/ai-anthropic4.0.5, 4.0.6@effect/ai-openai4.0.5, 4.0.6@effect/ai-openai-compat4.0.5, 4.0.6
- 主要言語
- TypeScript
- スター
- 16.7k
- フォーク
- 808
- 平均マージ
- 11時間 3分
- マージ済み PR(30日)
- 840
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Effect-TS/effect のほかの issue
-
PgClient: withTransaction dies instead of failing with SqlError when the server ends the session対応中かも @effect-bot が今日担当しました。 オープン
Effect-TS/effect#10290 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
shorthand for mapping over tagged union schema対応中かも @effect-bot が今日担当しました。 オープンenhancement
Effect-TS/effect#10286 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
Cluster: A way to remove processed requests from storage対応中かも @effect-bot が 2 日前に担当しました。 オープンenhancement
Effect-TS/effect#10239 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 47/100
メンテナーはふだん 1 日以内に返信
Effect-TS/effect の issue をすべて見る
似ている issue
-
🐛 bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
LibreChat-AI/LibreChat#17733 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
TypeStrong/typedoc#3150 ·
-
accessibility bug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
Normalize image-source MIME types before SVG dispatch対応中かも @HarsH-1877 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
tscircuit/image-utils#58 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
メンテナーはふだん 2 日以内に返信