Register generate_chart as an Anthropic tool so the agent can produce charts#78
Merged
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Beta preview has been cleaned up because this PR was closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Charts have never worked end-to-end. The frontend's
extractChartSpecsand chart components already render ```chart fenced JSON correctly, andgenerate_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.generate_chartentry toTOOL_DEFINITIONS, placed last so it inherits thecache_controlbreakpoint set by_tool_defs_for_anthropicinroutes/chatbot.py. Theinput_schemamirrors the function signature; required keys arechart_type, title, data, x_field, y_fields. Format enums (currency, percent, percent_decimal, number, compact, year) match the frontend'sAxisConfig.execute_tool.CHARTS:section toSYSTEM_PROMPTexplaining the contract: call the tool, then paste the returnedchart_markdownverbatim into the next text response, and don't try matplotlib inrun_python.Deliberately not touched:
run_python's sandboxallowed_globals— chart generation should go through the dedicated tool, not the Python sandbox.CHARTS_MODE_DIRECTIVEin PR Add Charts toggle chip beside Plan mode to bias responses toward visualizations #68.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— existingTestGenerateChartandtest_dispatches_generate_chartshould pass unchanged (they were already in the suite; this PR just makes the dispatch route real).generate_chartand includes the resulting ```chart block in its reply, and that the frontend renders it._tool_defs_for_anthropicshould still attachcache_controlonly to the last tool (nowgenerate_chart).🤖 Generated with Claude Code