Skip to content

API Reference parrot.registry.routing.llm_helper

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.registry.routing.llm_helper

View source

Shared LLM-route helper utilities (FEAT-111 Module 3).

Extracted from IntentRouterMixin._parse_invoke_response to be reused by both the strategy-level router and the new store-level StoreRouter.

Usage::

from parrot.registry.routing import extract_json_from_response, run_llm_ranking

raw_dict = extract_json_from_response(ai_message)
result = await run_llm_ranking(bot.invoke, prompt, timeout_s=1.0)

Function extract_json_from_response

Extract the first JSON object from an LLM response.

Supports:

  • Objects with a .output attribute (AIMessage style).
  • Objects with a .content attribute.
  • Plain str — the first {...} block is extracted.
  • Plain dict — returned as-is.
  • Any other type / unparseable input → None.

extract_json_from_response(response: Any) -> Optional[dict]

Parameter Type Default Description
response Any Raw response from invoke() or a test fixture.

Returns: Optional[dict] — A parsed dict, or None when parsing fails.

Function run_llm_ranking

Call invoke_fn with prompt, apply a timeout, and parse JSON output.

On timeout, exception, or un-parseable output the function logs a WARNING and returns None. It never raises.

run_llm_ranking(invoke_fn: Callable, prompt: str, timeout_s: float) -> Optional[dict]

Parameter Type Default Description
invoke_fn Callable An async callable that accepts a str prompt and returns an AIMessage-like object.
prompt str The prompt to pass to the LLM.
timeout_s float Maximum seconds to wait for the LLM response.

Returns: Optional[dict] — Parsed dict from the LLM response, or None on failure.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally