Skip to main content
Use Code Mode in the Agent API to find and fetch PyPI release pages for your dependencies in one request. The model writes a JavaScript program that keeps only the evidence needed for a table of versions, dates, and source URLs.

What you need

Install httpx in a virtual environment and set your API key. The script sends HTTP requests with httpx because version 0.50.0 of the Perplexity Python library does not have types for the Code Mode tool and output items.

How the request works

The script sends ten package names to POST /v1/agent. It adds programmatic_tool_calling to tools and puts programmatic in allowed_callers on web_search and fetch_url. The instructions ask the model to write one program. The program searches for each package with tools.search_web and Promise.all, then fetches the matching PyPI pages with tools.fetch_url. It calls text() with the page URLs, version headings, and release dates instead of the full tool results. The response contains the JavaScript in a program item and its status in a program_output item with the same call_id. The script reads the table from the assistant’s message, not from either program item. The request’s JSON Schema defines the table fields, and the script checks the package order, date format, and source URLs.

Run the script

Save this script as release_watchlist.py in your working directory.
release_watchlist.py
Run the script to print the generated JavaScript, the program status, the release table, and request usage.
If the model cannot verify a release, the script prints unverified for that row’s version, date, and URL. The script does not check whether a release is compatible with your project.

Compare with direct tool calls

Use --compare to send a second request with the same model, tools, package list, and output schema. The second request omits the Code Mode marker, caller restrictions, and program instructions.
The script prints both tables and the request metrics side by side. It reads tool counts from usage.tool_calls_details and total cost from usage.cost.total_cost. In this recorded run, both requests returned the same ten rows. The program sent the model only the selected evidence, while direct tool calls sent fetched page text. Results and metrics can change on each run. Check the release rows before you compare the request metrics.

Next steps

  • See Code Mode for the allowed_callers rules, the response items, streaming events, and error messages.
  • See Fetch URL Content and Web Search for the two tools that the program calls.