Skip to content

API Reference parrot.a2a.router

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

parrot.a2a.router

View source

A2A Proxy Router - Routes requests to remote A2A agents without LLM processing.

This module provides a gateway/proxy for routing requests to multiple A2A agents based on configurable rules. Unlike LLM-based orchestration, routing decisions are made using deterministic rules (skill matching, tag matching, regex patterns) resulting in minimal latency and zero LLM costs.

Alias A2AClient

Re-exported from parrot.a2a.client.A2AClient.

Alias A2AMeshDiscovery

Re-exported from parrot.a2a.mesh.A2AMeshDiscovery.

Class A2AProxyRouter

Proxy/Gateway for routing requests to A2A agents without LLM processing.

This router receives requests and forwards them to appropriate downstream agents based on configurable routing rules. No LLM is involved in the routing decision - it's pure rule-based matching.

The router can also expose itself as an A2A-compliant server, presenting an aggregated view of all downstream agents' capabilities.

Parameter Type Default Description
mesh A2AMeshDiscovery A2AMeshDiscovery instance for agent lookup
name str 'A2ARouter' Name for this router (used in AgentCard)
description str 'A2A Proxy Router' Description for this router
version str '1.0.0' Version string
aggregate_skills bool True If True, aggregate skills from all downstream agents
skill_prefix_with_agent bool True If True, prefix aggregated skills with agent name
default_timeout float 60.0 Default timeout for proxied requests
base_path str '/a2a' URL prefix for A2A endpoints when exposed as server
tags Optional[List[str]] None Tags for the router's AgentCard
capabilities Optional[AgentCapabilities] None Capabilities to advertise

Attributes:

Attribute Type Description
stats ProxyStats Get current statistics.

Method A2AProxyRouter.add_route

Add a routing rule.

add_route(pattern: str, target: Union[str, List[str]], *, strategy: RoutingStrategy = RoutingStrategy.SKILL_MATCH, priority: int = 0, load_balance: LoadBalanceStrategy = LoadBalanceStrategy.FIRST_HEALTHY, weights: Optional[Dict[str, float]] = None, transform_request: Optional[TransformFunc] = None, transform_response: Optional[ResponseTransformFunc] = None, enabled: bool = True, metadata: Optional[Dict[str, Any]] = None) -> 'A2AProxyRouter'

Parameter Type Default Description
pattern str Pattern to match (interpretation depends on strategy)
target Union[str, List[str]] Agent name or list of agent names
strategy RoutingStrategy RoutingStrategy.SKILL_MATCH How to match the pattern
priority int 0 Rule priority (higher = evaluated first)
load_balance LoadBalanceStrategy LoadBalanceStrategy.FIRST_HEALTHY How to select among multiple targets
weights Optional[Dict[str, float]] None Weights for weighted load balancing
transform_request Optional[TransformFunc] None Async function to transform request
transform_response Optional[ResponseTransformFunc] None Async function to transform response
enabled bool True Whether rule is active
metadata Optional[Dict[str, Any]] None Additional metadata

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.route_by_skill

Add routing rule that matches by skill ID.

route_by_skill(skill_id: str, target: Union[str, List[str]], **kwargs) -> 'A2AProxyRouter'

Parameter Type Default Description
skill_id str Skill ID to match
target Union[str, List[str]] Target agent(s)
**kwargs {} Additional RoutingRule parameters

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.route_by_skill_name

Add routing rule that matches by skill name (partial match).

route_by_skill_name(skill_name: str, target: Union[str, List[str]], **kwargs) -> 'A2AProxyRouter'

Parameter Type Default Description
skill_name str Skill name pattern to match
target Union[str, List[str]] Target agent(s)
**kwargs {} Additional RoutingRule parameters

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.route_by_tag

Add routing rule that matches by tag.

route_by_tag(tag: str, target: Union[str, List[str]], **kwargs) -> 'A2AProxyRouter'

Parameter Type Default Description
tag str Tag to match
target Union[str, List[str]] Target agent(s)
**kwargs {} Additional RoutingRule parameters

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.route_by_regex

Add routing rule that matches by regex pattern in the message.

route_by_regex(pattern: str, target: Union[str, List[str]], **kwargs) -> 'A2AProxyRouter'

Parameter Type Default Description
pattern str Regex pattern to match against message content
target Union[str, List[str]] Target agent(s)
**kwargs {} Additional RoutingRule parameters

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.route_round_robin

Add round-robin routing across multiple agents.

All requests matching this rule will be distributed evenly across the specified agents.

route_round_robin(agents: List[str], **kwargs) -> 'A2AProxyRouter'

Parameter Type Default Description
agents List[str] List of agent names to rotate through
**kwargs {} Additional RoutingRule parameters

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.set_default

Set the default agent for requests that don't match any rule.

set_default(agent_name: str) -> 'A2AProxyRouter'

Parameter Type Default Description
agent_name str Name of the default agent

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.remove_route

Remove a routing rule.

remove_route(pattern: str, strategy: Optional[RoutingStrategy] = None) -> bool

Parameter Type Default Description
pattern str Pattern of the rule to remove
strategy Optional[RoutingStrategy] None If provided, only remove rule with matching strategy

Returns: bool — True if rule was removed, False if not found

Method A2AProxyRouter.clear_routes

Clear all routing rules.

clear_routes() -> 'A2AProxyRouter'

Returns: 'A2AProxyRouter' — Self for method chaining

Method A2AProxyRouter.list_routes

List all configured routing rules.

list_routes() -> List[Dict[str, Any]]

Returns: List[Dict[str, Any]] — List of rule configurations

Method A2AProxyRouter.find_target

Find the target agent for a request.

Evaluates all rules in priority order and returns the first match.

find_target(message: str, *, skill_id: Optional[str] = None, tags: Optional[List[str]] = None) -> RoutingResult

Parameter Type Default Description
message str Message content
skill_id Optional[str] None Optional skill ID
tags Optional[List[str]] None Optional tags

Returns: RoutingResult — RoutingResult with matched rule and target agent

Method A2AProxyRouter.route_message

Route a message to the appropriate agent and return the response.

This is a PASSTHROUGH operation - no LLM is involved. The message is forwarded as-is to the matched agent.

route_message(message: str, *, skill_id: Optional[str] = None, tags: Optional[List[str]] = None, context_id: Optional[str] = None, metadata: Optional[Dict[str, Any]] = None, timeout: Optional[float] = None) -> Task

Parameter Type Default Description
message str Message content to route
skill_id Optional[str] None Optional skill ID to help with routing
tags Optional[List[str]] None Optional tags to help with routing
context_id Optional[str] None Optional context ID for conversations
metadata Optional[Dict[str, Any]] None Optional metadata
timeout Optional[float] None Optional timeout override

Returns: Task — Task with the response from the downstream agent

Raises:

  • ValueError — If no target agent found
  • ConnectionError — If target agent is unreachable

Method A2AProxyRouter.route_message_stream

Route a message and stream the response.

route_message_stream(message: str, *, skill_id: Optional[str] = None, tags: Optional[List[str]] = None, context_id: Optional[str] = None, metadata: Optional[Dict[str, Any]] = None) -> AsyncIterator[str]

Parameter Type Default Description
message str Message to route
skill_id Optional[str] None Optional skill ID
tags Optional[List[str]] None Optional tags
context_id Optional[str] None Optional context ID
metadata Optional[Dict[str, Any]] None Optional metadata

Returns: AsyncIterator[str]

Method A2AProxyRouter.invoke_skill

Invoke a specific skill on a remote agent.

If agent_name is not provided, finds an agent with the skill.

invoke_skill(skill_id: str, params: Optional[Dict[str, Any]] = None, *, agent_name: Optional[str] = None, context_id: Optional[str] = None) -> Any

Parameter Type Default Description
skill_id str ID of the skill to invoke
params Optional[Dict[str, Any]] None Parameters for the skill
agent_name Optional[str] None Optional specific agent to use
context_id Optional[str] None Optional context ID

Returns: Any — Skill result from the downstream agent

Raises:

  • ValueError — If no agent found with the skill

Method A2AProxyRouter.close_clients

Close all cached client connections.

close_clients() -> None

Method A2AProxyRouter.ask

Shortcut: send message and get response as string.

ask(message: str, *, agent: Optional[str] = None, **kwargs) -> str

Parameter Type Default Description
message str Message to send
agent Optional[str] None Optional specific agent to use
**kwargs {} Additional arguments for route_message

Returns: str — Response text

Method A2AProxyRouter.fan_out

Send message to multiple agents in parallel.

fan_out(message: str, agents: List[str], *, timeout: float = 60.0, **kwargs) -> Dict[str, Union[str, Exception]]

Parameter Type Default Description
message str Message to send
agents List[str] List of agent names
timeout float 60.0 Timeout for each request
**kwargs {} Additional arguments

Returns: Dict[str, Union[str, Exception]] — Dict mapping agent name to response or exception

Method A2AProxyRouter.pipeline

Execute a sequential pipeline of agents.

Each agent's output becomes the next agent's input.

pipeline(message: str, agents: List[str], **kwargs) -> str

Parameter Type Default Description
message str Initial message
agents List[str] Ordered list of agent names
**kwargs {} Additional arguments

Returns: str — Final output from the last agent

Method A2AProxyRouter.get_agent_card

Get the AgentCard for this router.

When aggregate_skills is True, the card includes skills from all downstream agents, making this router appear as a single agent with all capabilities.

get_agent_card(force_refresh: bool = False) -> AgentCard

Parameter Type Default Description
force_refresh bool False Force refresh of cached card

Returns: AgentCard — AgentCard representing this router

Method A2AProxyRouter.setup

Mount the router as an A2A server on an aiohttp application.

This allows external services to discover and use this router as if it were a regular A2A agent.

setup(app: web.Application, base_path: Optional[str] = None) -> None

Parameter Type Default Description
app web.Application aiohttp Application
base_path Optional[str] None Optional path prefix override

Method A2AProxyRouter.get_info

Get detailed information about the router state.

get_info() -> Dict[str, Any]

Returns: Dict[str, Any]

Alias AgentCapabilities

Re-exported from parrot.a2a.models.AgentCapabilities.

Alias AgentCard

Re-exported from parrot.a2a.models.AgentCard.

Alias AgentInterface

Re-exported from parrot.a2a.models.AgentInterface.

Alias AgentSkill

Re-exported from parrot.a2a.models.AgentSkill.

Class LoadBalanceStrategy

Strategy for load balancing across multiple target agents.

Class ProxyStats

Statistics for the proxy router.

Parameter Type Default Description
requests_total int 0
requests_routed int 0
requests_failed int 0
requests_no_match int 0
total_latency_ms float 0.0
agents_used Dict[str, int] dict()
rules_matched Dict[str, int] dict()
last_request_time Optional[datetime] None

Fields:

Field Type Default Description
requests_total int 0
requests_routed int 0
requests_failed int 0
requests_no_match int 0
total_latency_ms float 0.0
agents_used Dict[str, int] field(default_factory=dict)
rules_matched Dict[str, int] field(default_factory=dict)
last_request_time Optional[datetime] None

Attributes:

Attribute Type Description
avg_latency_ms float Average latency in milliseconds.

Method ProxyStats.to_dict

Convert stats to dictionary.

to_dict() -> Dict[str, Any]

Returns: Dict[str, Any]

Alias RegisteredAgent

Re-exported from parrot.a2a.mesh.RegisteredAgent.

Attribute ResponseTransformFunc

Class RoutingResult

Result of a routing decision.

Parameter Type Default Description
matched bool
rule Optional[RoutingRule] None
target_agent Optional[RegisteredAgent] None
match_details Dict[str, Any] dict()

Fields:

Field Type Default Description
matched bool
rule Optional[RoutingRule] None
target_agent Optional[RegisteredAgent] None
match_details Dict[str, Any] field(default_factory=dict)

Class RoutingRule

Defines a routing rule for matching requests to agents.

Parameter Type Default Description
pattern str
strategy RoutingStrategy
target_agents List[str] list()
priority int 0
load_balance LoadBalanceStrategy LoadBalanceStrategy.FIRST_HEALTHY
weights Optional[Dict[str, float]] None
transform_request Optional[TransformFunc] None
transform_response Optional[ResponseTransformFunc] None
enabled bool True
metadata Dict[str, Any] dict()
_compiled_regex Optional[re.Pattern] None
_round_robin_counter int 0
_last_used Dict[str, datetime] dict()

Fields:

Field Type Default Description
pattern str Pattern to match (skill_id, tag, regex, or "*" for default)
strategy RoutingStrategy How to match the pattern against requests
target_agents List[str] field(default_factory=list) List of agent names that can handle matching requests
priority int 0 Rule priority (higher = evaluated first)
load_balance LoadBalanceStrategy LoadBalanceStrategy.FIRST_HEALTHY Strategy for selecting among multiple targets
weights Optional[Dict[str, float]] None Optional weights for weighted load balancing
transform_request Optional[TransformFunc] None Optional async function to transform request before sending
transform_response Optional[ResponseTransformFunc] None Optional async function to transform response before returning
enabled bool True Whether this rule is active
metadata Dict[str, Any] field(default_factory=dict) Additional metadata for the rule

Class RoutingStrategy

Strategy for selecting target agent.

Alias Task

Re-exported from parrot.a2a.models.Task.

Alias TaskState

Re-exported from parrot.a2a.models.TaskState.

Attribute TransformFunc

Alias serialize_task_state

Re-exported from parrot.a2a.models.serialize_task_state.

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