[BUG][Conversation][return_messages_as_list returns formatted strings, not messages, and one caller annotates it as List[dict]]
bug
## Summary
`Conversation.return_messages_as_list()` sounds like the accessor that returns a chat-completions message list. It returns a list of **formatted strings** instead. Callers reaching for "the message list" get a flattener, and at least one call site annotates the return type incorrectly as a result.
## Where
`swarms/structs/conversation.py:1269-1278`:
```python
def return_messages_as_list(self):
"""Return the conversation messages as a list of formatted strings.
Returns:
list: List of messages formatted as 'role: content'.
"""
return [
f"{message['role']}: {message['content']}"
for message in self.conversation_history
]
```
The real accessor is the next method, `return_messages_as_dictionary` (`:1280-1292`):
```python
return [
{
"role": message["role"],
"content": message["content"],
}
for message in self.conversation_history
]
```
## The mislabelled call site
`swarms/structs/debate_with_judge.py:528-535`:
```python
def get_conversation_history(self) -> List[dict]:
...
return self.conversation.return_messages_as_list()
```
Annotated `List[dict]`; returns `List[str]`. Any caller trusting the annotation and doing `msg["role"]` gets `TypeError: string indices must be integers`.
## Why it matters now
Across all multi-agent structures, `return_messages_as_dictionary` — the one method that yields real `{"role", "content"}` dicts — is called by exactly one place in the repo (`swarms/structs/transforms.py:497`). Meanwhile every structure flattens its conversation by hand (#2029).
A developer fixing that flattening will grep for a message-list accessor, find `return_messages_as_list`, and get strings — reproducing the bug they set out to fix. The naming actively points away from the correct API.
## Suggested fix
Options, roughly in order of preference:
1. Rename to reflect behaviour — `return_messages_as_strings()` — and keep `return_messages_as_list` as a deprecated alias for one release.
2. Or make `return_messages_as_list` return dicts (the name-obvious behaviour) and move the string form to a clearly-named method. This is a breaking change for any external caller, so it needs a release note.
Either way, fix the `List[dict]` annotation at `debate_with_judge.py:528`.
Worth pairing with a short docstring on `return_messages_as_dictionary` noting it is the one to use when building a request body.
Related: #2029, #2030.
Found at `3e89f27b` (v14.0.2).
关闭于 1 天前 0 条评论