{"record":{"id":"611f66fbe0978852","repo":"BerriAI/litellm","slug":"task-task-id-ended-with-state-state","errorCode":null,"errorMessage":"Task {task_id} ended with state: {state}","messagePattern":"Task (.+?) ended with state: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/a2a_protocol/providers/pydantic_ai_agents/transformation.py","lineNumber":147,"sourceCode":"                json=poll_request,\n                headers={\n                    **(agent_extra_headers or {}),\n                    \"Content-Type\": \"application/json\",\n                },\n            )\n            response.raise_for_status()\n            poll_data = _STR_KEY_DICT_ADAPTER.validate_python(response.json())\n\n            result = _STR_KEY_DICT_ADAPTER.validate_python(poll_data.get(\"result\", {}))\n            status = _STR_KEY_DICT_ADAPTER.validate_python(result.get(\"status\", {}))\n            state = status.get(\"state\", \"\")\n\n            verbose_logger.debug(\"Pydantic AI: Poll attempt %s/%s, state=%s\", attempt + 1, max_attempts, state)\n\n            if state == \"completed\":\n                return poll_data\n            elif state in (\"failed\", \"canceled\"):\n                raise Exception(f\"Task {task_id} ended with state: {state}\")\n\n            await asyncio.sleep(poll_interval)\n\n        raise TimeoutError(f\"Task {task_id} did not complete within {max_attempts * poll_interval} seconds\")\n\n    @staticmethod\n    async def _send_and_poll_raw(\n        api_base: str,\n        request_id: str,\n        params: \"_SupportsModelDump | _SupportsPydanticDict | Mapping[str, object]\",\n        timeout: float = 60.0,\n        agent_extra_headers: dict[str, str] | None = None,\n    ) -> dict[str, object]:\n        \"\"\"\n        Send a request to Pydantic AI agent and return the raw task response.\n\n        This is an internal method used by both non-streaming and streaming handlers.\n        Returns the raw Pydantic AI task format with history/artifacts.","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/a2a_protocol/providers/pydantic_ai_agents/transformation.py#L129-L165","documentation":"Generic Exception raised while polling a Pydantic AI task: the task's status.state became 'failed' or 'canceled' before reaching 'completed'. The message echoes the task id and terminal state from the agent's response.","triggerScenarios":"PydanticAITransformation polling loop observes state='failed' or 'canceled' in result.status.state of the polled task before completion (agent raised an error, tool crashed, or the task was canceled).","commonSituations":"Bugs in the deployed Pydantic AI agent (invalid tools, missing API keys in the agent env); user- or system-initiated cancellation; agent runtime OOM/timeout surfacing as failed state.","solutions":["Inspect the Pydantic AI agent's logs/servers for the root cause of the failed task","Validate the request payload (message, tools config) against what the agent accepts","If cancellation is expected, handle this exception and surface it as a user-facing cancellation instead of an error"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = await PydanticAIHandler.handle_non_streaming(...)\nexcept Exception as e:\n    msg = str(e)\n    if \"ended with state:\" in msg:\n        state = msg.rsplit(\":\", 1)[-1].strip()\n        if state == \"canceled\":\n            return cancellation_response()\n        return agent_error_response(detail=msg)  # surface agent-side failure\n    raise","preventionTips":["Monitor Pydantic AI agent logs to catch failing tasks at the source","Include the task id from the error when reporting so operators can look it up","Validate agent tool/API-key configuration before sending production traffic"],"tags":["a2a","litellm","pydantic-ai","task-failed","polling","agent-state"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}