Skip to content

Verify that a figure can ride on a tool result - #421

Merged
neuromechanist merged 10 commits into
developfrom
spike/tool-result-image-transport
Sep 20, 2026
Merged

neuromechanist merged 10 commits into
developfrom
spike/tool-result-image-transport

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Closes the one open question in .context/browser-execution-tool-design.md that the note itself flagged as asserted without evidence: whether the provider layer accepts image content blocks on a tool message. It decides whether plots reach the model at all, so it decides the shape of Phase 1's tool-result contract and of Phase 2's widget. Answering it before Phase 1 starts is cheaper than discovering it inside Phase 2.

What was measured

It works, and all four plausible spellings converge.

An image block on a ToolMessage leaves this process as an Anthropic image block nested inside the tool_result. The Anthropic native block, LangChain's v1 and v0 standard image blocks, and an OpenAI-style data: URL all normalize to the same payload, so a widget cannot pick the wrong spelling and silently drop the figure. Against the live endpoint, both offered models read a chart returned on a tool result.

Two findings that change what Phase 2 has to do:

  • Nothing in the client stack validates the media type. image/svg+xml passes every layer here and comes back as a 400 from the endpoint. savefig defaults to PNG, but SVG is the usual choice when a figure is bound for a web page, so a recipe that picks it would fail at request time, after the analysis has already run. The accepted set is now declared as IMAGE_MEDIA_TYPES in src/core/services/anthropic_models.py, and the producer has to gate on it.
  • An image can also travel as a URL, which would have the model's provider fetch it. Inline base64 is what preserves this design's property that data-plane bytes never move through a third party, and it is what the tests pin.

How it is tested

tests/test_core/test_tool_result_image_transport.py runs everywhere and asserts the real payload CachingChatAnthropic builds, through the real _get_request_payload every generate and stream path calls, following the precedent in tests/test_core/test_anthropic_llm.py. It also pins that no cache marker lands inside a tool result, which is the interaction that would break if the caching strategy moved onto message content.

tests/test_integration/test_anthropic_platform.py::TestToolResultImages settles the half no local test can. Each offered model is asked about two bar charts that differ only in which bar is tallest and which is shortest; the text around them is byte-identical, so an answer that tracks the picture can only have come from the picture, and a model emitting a fixed guess can match at most one chart. That replaces an earlier no-image control which depended on a coincidence not happening. The answer comes back as the arguments of a forced tool call rather than as prose, so no reply style can produce a wrong answer or a false alarm about the transport.

Every assertion was mutation-checked. Inverting the image comparison fails all four spellings; turning the unknown-block control into a real image spelling fails that control; narrowing IMAGE_MEDIA_TYPES fails the drift test against the SDK; making the two chart fixtures agree fails the guard that keeps the pair meaningful.

What the live runs cost, and what they taught

Five dispatches, and the first four failed on the fixture rather than on the transport. Each failure looked like a transport failure and was not:

  • A three-digit number in a 5x7 bitmap font came back as 724, then 704 after the glyph was redrawn. First and last digit right both times, so the picture was arriving; five-pixel glyphs do not survive the downscaling an image goes through on its way into a model.
  • A prompt asking for "two digits and nothing else" met a numbered list, 1. Bar 1: Medium height ... 5. Bar 5: Tallest, which read the chart perfectly and parsed to the wrong answer. A prompt asking for a format is a request; a tool schema is a structure.
  • Asked about a seven-bar chart with no image at all, claude-haiku-4-5 produced exactly the fixture's answer. Nothing in that request depends on the fixture, so it was not a leak, but a guess that matches the fixture makes a one-chart test unfalsifiable. Hence two charts.
  • Asked about a four-bar chart, the same model named the tallest bar correctly and reported the shortest as bar 5. There is no bar 5. Perception was never the problem; indexing was. The prompt now states how many bars there are, which leaks nothing because which bar is tallest lives only in the picture.

tests/helpers/images.py writes the PNG by hand, with no image dependency: the repository has no matplotlib or Pillow, and adding one to draw four rectangles would be a poor trade.

Notes

  • The live tests run on push to main and on workflow_dispatch, not on pull requests, which is this repository's existing cost policy. Dispatches on this branch are how the live half was confirmed; the final one is green on all four cases.
  • Unrelated and pre-existing: tests/test_cli/test_main.py::TestVersionCommand::test_version_shows_version fails under Python 3.14 because rich highlights the version number with ANSI codes. It fails the same way on unmodified develop, and CI runs 3.11, 3.12 and 3.13, so it is invisible there.

The browser execution design has execute_code return its plots on the
tool result and lists that as an open question, stated without being
verified. It holds: an image block on a ToolMessage leaves this process
as an Anthropic image block nested in the tool_result, and all four
plausible spellings normalize to the same thing, so a widget cannot pick
the wrong one and silently lose the figure.

Nothing in the client stack validates the media type, so SVG (what
matplotlib produces by default for the web) passes every layer here and
fails as a 400 from the endpoint after the analysis has run. The accepted
set is now declared as IMAGE_MEDIA_TYPES, next to the model tables so a
CLI-only install can reach it, with a test comparing it against the
anthropic SDK's own declaration.

Offline tests assert the real payload CachingChatAnthropic builds; the
live test sends one real request and reads a number drawn in the picture
and written nowhere else, with a control round trip that omits the
picture.
Records what was measured and the two consequences for Phase 2: the
widget gates the media type because nothing else does, and images travel
as inline base64 rather than as a URL the provider would fetch from the
data plane.
The first live run replied 724 for a figure drawn as 734. A wrong digit
is proof the picture arrived, so the transport is fine and the font was
not: a flat-topped seven-segment 3 is a 2 once the image has been
downscaled. Rounded glyph, larger cells, and the finding recorded where
the next person will read it.
Two live runs replied 724 and then 704 for a figure drawn as 734, both
with the first and last digit right. That is the picture arriving and
being read; five-pixel-wide glyphs are just not a reliable channel once
an image has been downscaled. Which bar is tallest does not degrade that
way, and it is closer to what this runtime will be asked about.
Moves the SDK drift test next to the constant it guards, in
test_anthropic_models.py, where a maintainer editing IMAGE_MEDIA_TYPES
will look and where the module's other anti-drift guard already lives.

The live test now runs against every offered model rather than Haiku
alone: the design's premise is that plots reach the model a person
picked, and driving OFFERED_MODELS means a third one cannot skip it. Its
reply parsing separates 'answered in prose' from 'named the wrong bar',
so a red dispatch is triaged as the instruction-following slip it would
be rather than as a dropped image. Seven bars instead of five halves the
control's own coincidence rate, from 1 in 20 to 1 in 42.

Also asserts the CachingChatAnthropic type before reaching for
_get_request_payload, as the precedent does; records that the cache
marker test is a forward guard whose invariant currently holds for a
reason outside the tool-result path; and stops claiming SVG is
matplotlib's default, which it is not.
The live run across both offered models found the flakiness the review
predicted, and found it in the most instructive way: sonnet-5 passed
both tests, and haiku-4-5 read the chart perfectly while failing the
test. It answered with a numbered list, '1. Bar 1: Medium height ... 5.
Bar 5: Tallest', so the first digit in the reply belonged to the list
rather than to a bar.

A prompt asking for a format is a request. A tool schema is a structure.
The model now reports through a forced report_bars(tallest_position,
shortest_position) call and the test reads its arguments, so no reply
style can produce a wrong answer or a false alarm about the transport.
The live control caught something a passing test would have hidden.
Asked to report the bars with no image attached at all, haiku-4-5
answered 'tallest 5, shortest 2', which was exactly the fixture's
answer. Nothing in that request depends on the fixture, so it is not a
leak, it is the model's prior; but a prior that matches the fixture
makes a single-chart test unfalsifiable for that model, passing whether
or not the picture arrived.

Two charts fix it. They carry byte-identical text and differ only in
which bar is tallest and which is shortest, so a fixed guess can match
at most one and answering both correctly is what proves the figure is
being read. That subsumes the no-image control, which relied on a 1 in
42 coincidence not happening, and removes it.

The fixtures move next to the encoder that draws them and the expected
answer is derived from the heights rather than written beside them. The
one property the pair depends on, that the two charts disagree, is
checked in the offline suite, which runs on every pull request, rather
than in a module skipped whenever no API key is present.
The third live run failed for the third fixture reason in a row, and
again not for a transport reason. On a seven-bar chart haiku-4-5
reported bar 4 as the tallest when bar 5 was 1.0 and bar 4 was 0.30,
named the shortest bar correctly, and got BOTH answers right on the
other chart, which a model perceiving nothing cannot do. The picture
arrived and was read; the index was miscounted.

That makes the fixture the thing under test rather than the transport,
so the fixture gets easier instead of the assertion getting weaker: four
bars, counted reliably, with heights far apart. Sonnet-5 passed all four
cases before and after.

Both rejected fixtures shared one shape, now written down where the next
person will meet it: the model could see the picture and the fixture
could not carry the answer.
Asked about a four-bar chart, haiku-4-5 named the tallest bar correctly
and reported the shortest as bar 5. There is no bar 5. Two live runs
have now failed on indexing while getting the perception right, so the
prompt states the count: it leaks nothing, because which bar is tallest
lives only in the picture, and it removes the last part of the question
that is not about the figure.

Also drops the None branch of _report_bars, dead since the two-fixture
pair replaced the no-image control.
@neuromechanist
neuromechanist merged commit e2a449f into develop Sep 20, 2026
15 checks passed
@neuromechanist
neuromechanist deleted the spike/tool-result-image-transport branch September 20, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant