BerriAI/litellm · error · TimeoutError
Task did not complete within seconds
Error message
Task {task_id} did not complete within {max_attempts * poll_interval} seconds What it means
transformation.py polls the Pydantic AI agent's task endpoint up to max_attempts times, sleeping poll_interval seconds between tries. If the state never reaches 'completed' (and never hits failed/canceled) within that window, it raises TimeoutError with the total budget (max_attempts * poll_interval seconds). The task may still be running upstream — this is a client-side give-up, not proof of agent failure.
Solutions
- Increase the poll budget (max_attempts / poll_interval, or the timeout kwarg) for long-running agents
- Check the agent server to confirm whether the task eventually completes — if yes, only the budget needs raising
- Catch TimeoutError and retry the request or surface a 504-style response
- Optimize the agent (smaller context, faster tools) so tasks finish inside the window
Example fix
# before
result = await PydanticAIHandler.handle_non_streaming(
request_id=req_id, params=params, api_base=base, timeout=60.0
)
# after
try:
result = await PydanticAIHandler.handle_non_streaming(
request_id=req_id, params=params, api_base=base, timeout=300.0
)
except TimeoutError:
raise HTTPException(status_code=504, detail="Agent task timed out; retry or raise poll budget")
Defensive patterns
Strategy: retry
Validate before calling
def poll_budget_sufficient(expected_seconds: float, max_attempts: int, poll_interval: float) -> bool:
return max_attempts * poll_interval >= expected_seconds
Try / catch
try:
result = await PydanticAIHandler.handle_non_streaming(req_id, params, api_base, timeout=timeout)
except TimeoutError:
# task may still complete upstream; retry or surface 504
return JSONResponse(status_code=504, content={"detail": "agent task timed out"})
Prevention
- Size the poll budget (timeout / max_attempts) to your agent's worst-case task duration
- Track p99 task durations per agent and alert when they approach the budget
- Return 504 with retry guidance so callers back off instead of hard-failing
When it happens
Trigger: A long-running agent task (big prompt, slow tools, cold-started model) exceeding the poll budget; the agent server stalling mid-task so state stays 'working'/'submitted'; poll_interval/max_attempts left at defaults for workloads that need minutes.
Common situations: Heavy RAG or multi-tool agent runs against a modest polling budget; agent server under load responding slowly; network latency inflating each poll cycle; tasks that legitimately run longer than the default budget in production but not in tests.
Related errors
- Task ended with state
- api_base is required for Pydantic AI agents
- api_base is required for Pydantic AI agents
- api_base is required for PydanticAIProviderConfig
- Polling timed out after
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/7551961d6829a287.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/a2a_protocol/providers/pydantic_ai_agents/transformation.py:151
},
)
response.raise_for_status()
poll_data = _STR_KEY_DICT_ADAPTER.validate_python(response.json())
result = _STR_KEY_DICT_ADAPTER.validate_python(poll_data.get("result", {}))
status = _STR_KEY_DICT_ADAPTER.validate_python(result.get("status", {}))
state = status.get("state", "")
verbose_logger.debug("Pydantic AI: Poll attempt %s/%s, state=%s", attempt + 1, max_attempts, state)
if state == "completed":
return poll_data
elif state in ("failed", "canceled"):
raise Exception(f"Task {task_id} ended with state: {state}")
await asyncio.sleep(poll_interval)
raise TimeoutError(f"Task {task_id} did not complete within {max_attempts * poll_interval} seconds")
@staticmethod
async def _send_and_poll_raw(
api_base: str,
request_id: str,
params: "_SupportsModelDump | _SupportsPydanticDict | Mapping[str, object]",
timeout: float = 60.0,
agent_extra_headers: dict[str, str] | None = None,
) -> dict[str, object]:
"""
Send a request to Pydantic AI agent and return the raw task response.
This is an internal method used by both non-streaming and streaming handlers.
Returns the raw Pydantic AI task format with history/artifacts.
Args:
api_base: Base URL of the Pydantic AI agent
request_id: A2A JSON-RPC request IDView on GitHub (pinned to 77b7c6c40c)