tools.wolfram_alpha module
Query Wolfram|Alpha via the LLM API.
Uses https://www.wolframalpha.com/api/v1/llm-api which returns
plain-text results optimised for consumption by large language models.
Resolve the App ID via per-user key, WOLFRAM_ALPHA_APP_ID env var,
or api_keys.wolfram_alpha in config.yaml.
Optional: set WOLFRAM_ALPHA_USE_BEARER_AUTH=1 to send the App ID in the
Authorization: Bearer header instead of the appid query parameter.
- async tools.wolfram_alpha.run(query, maxchars=None, units=None, timeout=None, assumptions=None, languagecode=None, location=None, timezone=None, latlong=None, countrycode=None, currency=None, ip=None, scantimeout=None, formattimeout=None, parsetimeout=None, width=None, maxwidth=None, plotwidth=None, mag=None, ctx=None)[source]
Execute a Wolfram|Alpha LLM-API query and return a JSON-encoded result.
Entry point for the
wolfram_alpha_querytool. Validates the query, resolves the App ID, builds the request parameters, performs an async HTTP GET against the Wolfram|Alpha LLM API, and serialises either the plain-text answer or a structured error to a JSON string. HTTP error statuses are mapped to friendly messages (501 = uninterpretable input, 403 = bad App ID, 400 = missing input, other = generic), and network/unexpected failures are caught and returned aserrorpayloads rather than raised.Resolves credentials via
_resolve_app_id()(which checks the per-user key in Redis, theWOLFRAM_ALPHA_APP_IDenv var, then config), normalises the query with_normalize_query_input(), chooses the auth scheme with_use_bearer_auth(), and constructs the query pairs with_build_query_pairs(). It opens anaiohttp.ClientSessionto callLLM_API_URL, logs request/response activity, and on any non-200 status fires a fire-and-forget observability event through_emit_error_event(). When no App ID can be resolved it returnsmissing_api_key_error("wolfram_alpha")fromtools.manage_api_keys.This is invoked by the generic tool dispatcher in
tool_loader.py(which locates the module’srunattribute viagetattr(module, "run")) and directly by the tool’s tests intests/test_wolfram_alpha_tool.py.- Parameters:
query (
str) – Natural-language Wolfram|Alpha input; required and must be non-blank.maxchars (
int|None) – Max characters in the response (clamped 500-32000).timeout (
int|None) – Maps to Wolfram’stotaltimeout; bounded to[5, 30]seconds, defaulting toDEFAULT_TIMEOUT.assumptions (
list[str] |None) – Disambiguation tokens for re-tries.location (
str|None) – Location hint for locale-aware results.countrycode (
str|None) – Country code for regional defaults.currency (
str|None) – Preferred currency for financial queries.scantimeout (
int|None) – Scan-phase timeout in seconds (1-120).formattimeout (
int|None) – Format-phase timeout in seconds (1-120).parsetimeout (
int|None) – Parse-phase timeout in seconds (1-120).ctx (
ToolContext|None) – Tool context supplyingredis,user_id,channel_id, andconfigfor per-user App ID resolution; may beNone(falls back to env/config credentials).
- Returns:
A JSON string. On success it contains
queryandanswer; on failure it contains anerrorfield (plushttp_statusand optionaldetail/suggestionsfor HTTP errors).- Return type: