{"record":{"id":"2f144991597d8a0f","repo":"run-llama/llama_index","slug":"response-must-be-of-type-agentchatresponse-or-stre","errorCode":null,"errorMessage":"response must be of type AgentChatResponse or StreamingAgentChatResponse","messagePattern":"response must be of type AgentChatResponse or StreamingAgentChatResponse","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/instrumentation/events/agent.py","lineNumber":89,"sourceCode":"\n    Args:\n        response (Optional[AGENT_CHAT_RESPONSE_TYPE]): Agent chat response.\n\n    \"\"\"\n\n    response: Optional[AGENT_CHAT_RESPONSE_TYPE]\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_response(cls: Any, values: Any) -> Any:\n        \"\"\"Validate response.\"\"\"\n        response = values.get(\"response\")\n        if response is None:\n            pass\n        elif not isinstance(response, AgentChatResponse) and not isinstance(\n            response, StreamingAgentChatResponse\n        ):\n            raise ValueError(\n                \"response must be of type AgentChatResponse or StreamingAgentChatResponse\"\n            )\n\n        return values\n\n    @field_validator(\"response\", mode=\"before\")\n    @classmethod\n    def validate_response_type(cls: Any, response: Any) -> Any:\n        \"\"\"Validate response type.\"\"\"\n        if response is None:\n            return response\n        if not isinstance(response, AgentChatResponse) and not isinstance(\n            response, StreamingAgentChatResponse\n        ):\n            raise ValueError(\n                \"response must be of type AgentChatResponse or StreamingAgentChatResponse\"\n            )\n        return response","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/instrumentation/events/agent.py#L71-L107","documentation":"Pydantic model validator on AgentChatWithStepEndEvent: the optional `response` field must be None, an AgentChatResponse, or a StreamingAgentChatResponse. The whole-dict validator (mode='before') fires when the event is constructed (or deserialized) with any other type — typically a plain string, dict, or a ChatResponse from a raw LLM call. It protects downstream instrumentation consumers that assume agent-chat response semantics.","triggerScenarios":"AgentChatWithStepEndEvent(response='done', ...) or Event(...response=chat_response.message.content ...); emitting a custom end event from a workflow step and passing the underlying LLM ChatResponse instead of an AgentChatResponse; deserializing an event payload whose response field was downgraded to a string.","commonSituations":"Custom agents/workflows that manually dispatch instrumentation events; serializing events to JSON and back (response becomes a dict and fails revalidation); wrapping lower-level LLM APIs that return ChatResponse.","solutions":["Wrap text in AgentChatResponse before constructing the event: response=AgentChatResponse(response=text).","If you only have an LLM ChatResponse, convert it: AgentChatResponse(response=chat_response.message.content).","For streaming agents, pass the StreamingAgentChatResponse instance itself."],"exampleFix":"# before\nfrom llama_index.core.instrumentation.events.agent import AgentChatWithStepEndEvent\nev = AgentChatWithStepEndEvent(payload=..., response='final answer')  # raises\n\n# after\nfrom llama_index.core.agent.chat.types import AgentChatResponse\nev = AgentChatWithStepEndEvent(payload=..., response=AgentChatResponse(response='final answer'))","handlingStrategy":"type-guard","validationCode":"from llama_index.core.agent.chat.types import AgentChatResponse, StreamingAgentChatResponse\n\ndef valid_response(r):\n    return r is None or isinstance(r, (AgentChatResponse, StreamingAgentChatResponse))\n\nif not valid_response(resp):\n    resp = AgentChatResponse(response=str(resp))","typeGuard":"from llama_index.core.agent.chat.types import AgentChatResponse, StreamingAgentChatResponse\nfrom llama_index.core.instrumentation.events.agent import AGENT_CHAT_RESPONSE_TYPE\n\ndef is_agent_chat_response(r: object) -> bool:\n    return r is None or isinstance(r, (AgentChatResponse, StreamingAgentChatResponse))","tryCatchPattern":null,"preventionTips":["Never emit instrumentation events with raw strings or LLM ChatResponse objects","Convert through AgentChatResponse(response=...) before event construction","Type-annotate emitters with AGENT_CHAT_RESPONSE_TYPE"],"tags":["instrumentation","events","agent","pydantic"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}