Skip to content

API Reference parrot.tools.executors.abstract

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

parrot.tools.executors.abstract

View source

Abstract executor interface and serializable envelope.

The envelope is the contract that crosses the process boundary. Anything inside it must be JSON-serializable; anything not in it cannot be relied on by the remote runtime.

Alias AbstractTool

Re-exported from parrot.tools.abstract.AbstractTool.

Class AbstractToolExecutor

Pluggable transport that runs a tool somewhere other than here.

Concrete executors translate a :class:ToolExecutionEnvelope into whatever protocol the remote runtime speaks (HTTP, gRPC, k8s API, Redis Streams, etc.) and return a :class:ToolResult once the remote side finishes — either by waiting synchronously up to envelope.timeout_seconds or by returning a pending ToolResult and arranging for the final result to arrive via webhook.

Concrete implementations:

  • :class:LocalToolExecutor — in-process; reference / tests
  • :class:K8sToolExecutor — ephemeral Pod via kubernetes-asyncio
  • :class:QworkerToolExecutor — Qworker service (HTTP / Redis)

Method AbstractToolExecutor.execute

Run the tool described by envelope and return its ToolResult.

Implementations must:

  • raise :class:asyncio.TimeoutError (or a structured ToolResult(status="error", error="timeout")) when the remote side fails to respond within envelope.timeout_seconds.
  • never re-run permission checks. Those happen on the caller.
  • propagate envelope.trace_context to the worker so the trace stays connected.

execute(envelope: ToolExecutionEnvelope) -> 'ToolResult'

Parameter Type Default Description
envelope ToolExecutionEnvelope

Returns: 'ToolResult' — The final ToolResult, or a ToolResult(status="pending") with metadata["job_id"] set when the executor was constructed with a webhook delivery configuration.

Method AbstractToolExecutor.close

Release any pooled resources (HTTP sessions, k8s clients, etc.).

Idempotent: calling close() multiple times must not raise.

close() -> None

Alias PermissionContext

Re-exported from parrot.auth.permission.PermissionContext.

Class ToolExecutionEnvelope

The wire-format payload describing a single remote tool invocation.

Fields:

Field Type Default Description
tool_import_path str Dotted Python path of the tool class, formatted as "<module>:<qualname>" so the remote worker can do importlib.import_module(module) and getattr(cls).
tool_init_kwargs Dict[str, Any] Constructor arguments captured from the caller's instance. Forwarded as cls(**tool_init_kwargs) on the remote side. The executor kwarg is stripped before transit so the remote tool runs locally.
method_name Optional[str] None For ToolkitTool envelopes, the name of the bound method to invoke on the reconstructed toolkit. None for plain AbstractTool subclasses.
arguments Dict[str, Any] Validated tool arguments (the kwargs that would normally be passed to _execute).
permission_context Optional[Dict[str, Any]] None JSON projection of the caller's PermissionContext. The remote side does NOT re-run permission checks — Layer 2 enforcement happens on the caller before the envelope is sent. This is informational.
trace_context Optional[Dict[str, Any]] None JSON projection of the parent span so the remote runtime can mint a child span and keep the trace connected.
timeout_seconds int 300 Maximum wall-clock seconds to wait for the remote runtime to return a result.
webhook_callback_url Optional[str] None When set, the executor returns immediately with a "pending" ToolResult; the remote runtime POSTs the final ToolResult to this URL when it completes. The webhook handler is registered separately.
envelope_version int 1 Schema version. Bumped when the contract changes in a backwards-incompatible way.

Alias ToolResult

Re-exported from parrot.tools.abstract.ToolResult.

Alias TraceContext

Re-exported from parrot.core.events.lifecycle.trace.TraceContext.

Function build_envelope_from_tool

Construct a ToolExecutionEnvelope from a tool instance.

The tool's _init_kwargs (captured by AbstractTool.__init__) travel as tool_init_kwargs. The executor kwarg is stripped so the remote-side reconstruction runs the tool locally inside the worker process.

For :class:ToolkitTool instances, the toolkit class is what gets imported on the remote side, not ToolkitTool itself; the method_name field tells the worker which method to call on the reconstructed toolkit. tool_init_kwargs then carries the toolkit's constructor arguments (not the ToolkitTool's).

For :class:AgentTool instances (Agents-as-Tools), the wrapped agent cannot travel as a live object; instead the agent's name is shipped as an agent_ref init kwarg and the worker reconstructs the agent from the agent registry (parrot.registry). This requires the agent to be registered under its name on the worker side too, and the worker environment to carry whatever credentials the sub-agent's LLM needs.

build_envelope_from_tool(tool: 'AbstractTool', arguments: Dict[str, Any], permission_context: 'PermissionContext | None' = None, trace_context: 'TraceContext | None' = None, timeout_seconds: int = 300, webhook_callback_url: Optional[str] = None) -> ToolExecutionEnvelope

Parameter Type Default Description
tool 'AbstractTool'
arguments Dict[str, Any]
permission_context 'PermissionContext | None' None
trace_context 'TraceContext | None' None
timeout_seconds int 300
webhook_callback_url Optional[str] None

Returns: ToolExecutionEnvelope

Raises:

  • ValueError — When the tool's class cannot be imported by path (e.g. defined in __main__), or when an AgentTool wraps an agent that is not resolvable through the agent registry. Such tools cannot be executed remotely.

Function project_permission_context

Project a PermissionContext into a JSON-safe dict.

Only stable, request-scoped fields are exported — no resolver, no live callables. Roles convert from frozenset → list. The trace context piggy-backs on its own field in the envelope, not on this projection, so it is omitted here.

project_permission_context(pctx: 'PermissionContext | None') -> Optional[Dict[str, Any]]

Parameter Type Default Description
pctx 'PermissionContext | None'

Returns: Optional[Dict[str, Any]]

Function project_trace_context

Project a TraceContext into a JSON-safe dict.

Returns None when tc is None so envelopes stay compact.

project_trace_context(tc: 'TraceContext | None') -> Optional[Dict[str, Any]]

Parameter Type Default Description
tc 'TraceContext | None'

Returns: Optional[Dict[str, Any]]

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