langchain-ai/deepagents · error · RuntimeError

task() requires an active ToolRuntime

Error message

task() requires an active ToolRuntime

What it means

`call_subagent_task_tool` implements the sandbox's `task()` function and requires an active `ToolRuntime` to dispatch the subagent. It emits `start`/`complete`/`error` lifecycle events through that runtime, so calling `task()` without one is unsupported and raises `RuntimeError`.

Source

Thrown at libs/partners/quickjs/langchain_quickjs/_subagent.py:207


async def call_subagent_task_tool(
    task_tool: BaseTool,
    *,
    description: str,
    subagent_type: str,
    response_schema: dict[str, Any] | None,
    runtime: Any,
    label: str | None = None,
) -> Any:
    """Call the Deep Agents task tool and return a JavaScript-friendly value.

    This also emits `start` then `complete`/`error` subagent lifecycle
    events on the custom stream.
    """
    if runtime is None:
        msg = "task() requires an active ToolRuntime"
        raise RuntimeError(msg)

    parse_json_output = response_schema is not None
    if response_schema is not None:
        _validate_response_schema(response_schema)
        response_schema = _ensure_schema_title(response_schema)
        runtime = _runtime_with_response_format(runtime, response_schema)

    eval_id = getattr(runtime, "tool_call_id", None)
    stream_writer = getattr(runtime, "stream_writer", None)
    subagent_id = f"ptc_{task_tool.name}_{uuid.uuid4().hex[:8]}"

    runtime = _runtime_with_tool_call_id(runtime, subagent_id)

    start_event: SubagentStartEvent = {
        "type": SUBAGENT_STREAM_EVENT_TYPE,
        "phase": "start",
        "id": subagent_id,
        "subagent_type": subagent_type,

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Run `task()` only through the eval tool wired by the middleware so a ToolRuntime is always bound.
  2. In direct/test usage, construct and pass a real (or stub) ToolRuntime instead of None.
  3. If subagents are not needed, avoid exposing/calling the `task` tool in that configuration.

Example fix

// before
call_subagent_task_tool(prompt="do work", runtime=None)

// after
from langchain_quickjs._runtime import ToolRuntime
runtime = build_tool_runtime(tools=[...])
call_subagent_task_tool(prompt="do work", runtime=runtime)
Defensive patterns

Strategy: type-guard

Validate before calling

if runtime is None:
    raise RuntimeError("task() called without a ToolRuntime; wire the middleware first")

Type guard

def has_runtime(runtime) -> bool:
    return runtime is not None and hasattr(runtime, "run")

Try / catch

try:
    result = call_subagent_task_tool(prompt=prompt, runtime=runtime)
except RuntimeError as e:
    if "ToolRuntime" in str(e):
        initialize_subagent_middleware()

Prevention

When it happens

Trigger: Invoking `task()` from JavaScript inside the sandbox when the ToolRuntime was not injected/bound — e.g. calling the task tool wrapper directly with `runtime=None`, or running sandbox code in a context where the subagent middleware did not supply a runtime.

Common situations: Calling the internal `task` tool handler in unit tests or scripts without constructing a ToolRuntime; using the subagent feature without the middleware that wires the runtime into the REPL; a refactor dropped the runtime argument.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/5a7ec6fd9900dc08. Report an issue: GitHub.