[Bug] message.sender attribute is silently lost during serialization in the core execution loop
- Description Through static code review of the main execution loop, a data serialization defect was identified that causes the permanent loss of agent attribution in the conversation history.
When a completion is received, the code dynamically assigns the current agent's name to the message object: message.sender = active_agent.name Immediately following this, the message is serialized and appended to the history: history.append(json.loads(message.model_dump_json()))
Because OpenAI's ChatCompletionMessage is based on Pydantic, dynamically assigned attributes (like sender) that are not defined in the base schema are strictly excluded when model_dump_json() is called. Consequently, the sender information is completely stripped, breaking the traceability of multi-agent interactions.
Steps To Reproduce Note: This issue was identified via static program analysis.
Trace the execution flow inside the core while loop upon receiving a completion.
Note the dynamic assignment of message.sender.
Note the serialization method used before appending to history (model_dump_json()).
Evaluate the resulting dictionary appended to history; the sender key is entirely missing.
Expected Behavior The conversation history should accurately retain the sender field for every message. This is critical for maintaining context and knowing exactly which agent generated which response during multi-agent handoffs.
Actual Behavior The sender attribute is silently dropped by Pydantic's serialization mechanism, leaving the history payload without any agent attribution.
Impact Data Loss / Context Degradation: In multi-agent scenarios, losing the sender tag makes it impossible for downstream processes or the UI to reliably identify which agent produced a specific message in the shared history.
Proposed Remediation Reverse the order of operation: serialize the message into a dictionary first, and then inject the sender attribute into the dictionary before appending it to the history.
Suggested Fix:
msg_dict = json.loads(message.model_dump_json()) msg_dict["sender"] = active_agent.name history.append(msg_dict)
Source: openai/swarm