Skip to content

Register generate_chart as an Anthropic tool so the agent can produce charts#78

Merged
vahid-ahmadi merged 1 commit into
mainfrom
fix/register-generate-chart-tool
May 28, 2026
Merged

Register generate_chart as an Anthropic tool so the agent can produce charts#78
vahid-ahmadi merged 1 commit into
mainfrom
fix/register-generate-chart-tool

Conversation

@vahid-ahmadi
Copy link
Copy Markdown
Collaborator

Summary

Charts have never worked end-to-end. The frontend's extractChartSpecs and chart components already render ```chart fenced JSON correctly, and generate_chart() (`backend/agent_tools.py:541`) is implemented and tested — but the function was never advertised to the model as a tool, never routed in `execute_tool`, and never mentioned in the system prompt. This PR closes that loop.

  • Add a generate_chart entry to TOOL_DEFINITIONS, placed last so it inherits the cache_control breakpoint set by _tool_defs_for_anthropic in routes/chatbot.py. The input_schema mirrors the function signature; required keys are chart_type, title, data, x_field, y_fields. Format enums (currency, percent, percent_decimal, number, compact, year) match the frontend's AxisConfig.
  • Register the dispatch in execute_tool.
  • Add a short CHARTS: section to SYSTEM_PROMPT explaining the contract: call the tool, then paste the returned chart_markdown verbatim into the next text response, and don't try matplotlib in run_python.

Deliberately not touched:

Relationship to PR #68

PR #68 (Charts mode toggle) adds a directive telling the model to "use the available chart tools" — but none were registered. Once this PR lands and #68 rebases, #68 will start working as intended.

Closes #77

Test plan

  • cd backend && python -m pytest tests/test_agent_tools.py -k generate_chart -v — existing TestGenerateChart and test_dispatches_generate_chart should pass unchanged (they were already in the suite; this PR just makes the dispatch route real).
  • In a running session, ask a chart-worthy question (e.g. "Plot the marginal income tax schedule for 2025 from £0–£200k") and confirm the assistant calls generate_chart and includes the resulting ```chart block in its reply, and that the frontend renders it.
  • Verify prompt caching still works — _tool_defs_for_anthropic should still attach cache_control only to the last tool (now generate_chart).

🤖 Generated with Claude Code

The chart-rendering pipeline has been silently broken end-to-end: the
frontend's extractChartSpecs and chart components correctly parse and
render ```chart fenced JSON, and backend/agent_tools.py:541 has a
working generate_chart() with a test suite — but the function was never
wired into TOOL_DEFINITIONS or execute_tool, so the model had no way to
call it. Net effect: the agent could never produce a chart.

Changes:
- TOOL_DEFINITIONS: add a generate_chart entry at the END of the list
  (so _tool_defs_for_anthropic in routes/chatbot.py attaches the
  cache_control breakpoint to it). Mirrors the function signature —
  required: chart_type, title, data, x_field, y_fields; optional axis
  labels/formats/min-max, series styling, subtitle/source, and bar
  arrangement / line area_fill. format enums match the frontend's
  AxisConfig (currency, percent, percent_decimal, number, compact, year).
- execute_tool: route "generate_chart" to the generate_chart function.
- SYSTEM_PROMPT: add a short CHARTS section telling the model when to
  call the tool, that it must paste the returned chart_markdown
  verbatim into its next text response, and not to try matplotlib in
  run_python.

run_python's sandbox globals are intentionally left alone — chart
generation should go through the dedicated tool, not the Python
sandbox.

Relates to PR #68 (Charts mode toggle): its CHARTS_MODE_DIRECTIVE
currently tells the model to "use the available chart tools" but none
were registered. Once this lands and #68 rebases, that directive will
finally have something to point at.

Closes #77

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel Bot commented May 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
policyengine-uk-chat Ready Ready Preview, Comment May 28, 2026 10:42am

Request Review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

Beta preview has been cleaned up because this PR was closed.

@vahid-ahmadi vahid-ahmadi merged commit 11f4be5 into main May 28, 2026
4 checks passed
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.

Charts never render — generate_chart function exists but isn't registered as an agent tool

1 participant