Charts and Visualizations

Updated at:

FC Agent Sandbox Code Interpreter supports only the file-based fallback path for charts: generate chart files inside the sandbox, and then retrieve them through Filesystem. Do not rely on execution.results[].png or execution.results[].chart as the formal integration path.

Generate image files

When you use Matplotlib to generate a chart, save the image to a fixed path inside the sandbox:

sandbox.run_code("""import matplotlib.pyplot as plt; plt.plot([1, 2, 3], [1, 4, 9]);plt.savefig("/tmp/chart.png")""")
await sandbox.runCode(`import matplotlib.pyplot as plt; plt.plot([1, 2, 3], [1, 4, 9]); plt.savefig("/tmp/chart.png")`);

Then read the file contents:

content = sandbox.files.read("/tmp/chart.png", format="bytes")
const content = await sandbox.files.read("/tmp/chart.png", { format: "bytes" });

Alternatively, use an upload or download URL to deliver the image to a browser or a downstream system:

download_url = sandbox.download_url("/tmp/chart.png")
const downloadUrl = await sandbox.downloadUrl("/tmp/chart.png");

Access requirements for a download URL depend on the sandbox type. If the sandbox is a secure sandbox (the default creation setting), requesting the URL with a plain HTTP client that does not send an access token returns a 403 error: access denied: X-Access-Token header is required. In a secure sandbox, generating the URL does not make it anonymously accessible: each request must carry the sandbox access token in the X-Access-Token header, for example curl -H "X-Access-Token: <ACCESS-TOKEN>" "$DOWNLOAD_URL". To display the image in a browser, retrieve it on the application backend through sandbox.files.read(..., format="bytes") first, and then serve it to the frontend through the authenticated API of your application.

Generate multiple charts

Use stable filenames for multiple charts so they do not overwrite each other:

sandbox.run_code("""import matplotlib.pyplot as plt; plt.figure(); plt.plot([1, 2, 3]); plt.savefig("/tmp/chart-1.png"); plt.figure(); plt.bar(["A", "B"], [10, 20]); plt.savefig("/tmp/chart-2.png")""")
await sandbox.runCode(`import matplotlib.pyplot as plt; plt.figure(); plt.plot([1, 2, 3]); plt.savefig("/tmp/chart-1.png"); plt.figure(); plt.bar(["A", "B"], [10, 20]); plt.savefig("/tmp/chart-2.png")`);

Then read or download /tmp/chart-1.png and /tmp/chart-2.png separately.

Recommendations

  • When prompting AI to generate code, require plt.savefig() instead of plt.show().

  • Define an output directory and file naming convention, such as /tmp/chart.png or /tmp/charts/chart-1.png.

  • If you need long-term retention, write the images to Object Storage Service (OSS), a database, or application file storage.

  • For user-visible images, validate the file size and MIME type in your application.