fix(test): guard msg.metadata assignment for non-Message returns

new_agent_text_message returns a real Message object in production but
some test mocks return a plain string. Guard with hasattr + try/except
so the tool_trace assignment doesn't crash test_non_stream_events_ignored.
This commit is contained in:
rabbitblood 2026-04-22 16:24:55 -07:00
parent ed26f2733a
commit dcbcf19da1

View File

@ -411,8 +411,13 @@ class LangGraphA2AExecutor(AgentExecutor):
# immediately as the response (a2a_client.py reads .parts[0].text).
# Streaming: yielded as the last SSE event in the stream.
msg = new_agent_text_message(final_text, task_id=task_id, context_id=context_id)
if tool_trace:
msg.metadata = {"tool_trace": tool_trace}
# Attach tool_trace via metadata when supported. Guarded with
# hasattr because some test mocks return a plain string here.
if tool_trace and hasattr(msg, "metadata"):
try:
msg.metadata = {"tool_trace": tool_trace}
except (AttributeError, TypeError):
pass
await event_queue.enqueue_event(msg)
_result = final_text