Skip to content

Python: [Bug]: Message("user", "text") creates one text content per character #9326

Closed

Description

Description

Passing a plain string as contents to Message creates one text content per character instead of a single text content. With Chat Completions, every character is then sent as its own user message.

Message.__init__ declares contents: Sequence[Content | str | Mapping[str, Any]] | None and passes it straight to _parse_content_list, which iterates it. A str is itself a Sequence[str], so the type hint accepts Message("user", "Hello"), and iterating it yields single characters. The docstring says strings are auto-converted to text content, which only holds when they are wrapped in a list.

The repository uses this form too. python/samples/04-hosting/foundry-hosted-agents/responses/workflows/main.py builds Message("user", f"Create a {request.style} slogan for {request.topic}."), and 27 test call sites in the foundry, foundry_hosting and declarative packages do the same.

Expected: Message("user", "Write a slogan.") behaves like Message("user", ["Write a slogan."]), with one text content whose text is the whole string.

Code Sample

from agent_framework import Message
from agent_framework.openai import OpenAIChatCompletionClient

message = Message("user", "Write a slogan.")
print("contents:", len(message.contents), [content.text for content in message.contents][:5], "...")
print("text:    ", repr(message.text))

client = OpenAIChatCompletionClient(model="gpt-4o-mini", api_key="test-key")
payload = client._prepare_messages_for_openai([message])
print("chat completions messages:", len(payload), payload[:3], "...")

Output on main:

contents: 15 ['W', 'r', 'i', 't', 'e'] ...
text:     'W r i t e   a   s l o g a n .'
chat completions messages: 15 [{'role': 'user', 'content': 'W'}, {'role': 'user', 'content': 'r'}, {'role': 'user', 'content': 'i'}] ...

Error Messages / Stack Traces

None. The message is silently split.

Package Versions

agent-framework-core: 1.21.0 (source checkout of main at b53d25e)

Python Version

Python 3.13

Additional Context

  • Reproduced on Windows and Linux.
  • Fix: wrap a bare string in a list in Message.__init__ before parsing, so it becomes a single text content. I'll open a PR with this and regression tests.

Metadata

Metadata

Labels

pythonUsage: [Issues, PRs], Target: Python

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions