Skip to content

fix: ContextComponent.to_messages() returns smolagents-compatible list content#3330

Merged
Dallas98 merged 3 commits into
developfrom
fix/context-component-smolagents-content-format
Jun 29, 2026
Merged

fix: ContextComponent.to_messages() returns smolagents-compatible list content#3330
Dallas98 merged 3 commits into
developfrom
fix/context-component-smolagents-content-format

Conversation

@JasonW404

@JasonW404 JasonW404 commented Jun 29, 2026

Copy link
Copy Markdown
Member

Root Cause

Agent conversations fail with Error: wrong content: <system prompt text> when using non-modelengine model providers with enable_context_manager enabled.

Error chain:

core_agent.py → self.model(input_messages)
  → openai_llm.py → _prepare_completion_kwargs()
    → smolagents/models.py → get_clean_message_list()
      → assert isinstance(message.content, list), "Error: wrong content:" + str(message.content)

ContextComponent.to_messages() returned {"content": "string"} but smolagents v1.23 requires {"content": [{"type": "text", "text": "..."}]} when merging consecutive same-role messages.

The prepare_messages_for_smolagents_text_flattening mitigation in openai_llm.py only activated for model_factory == "modelengine", leaving all other providers (OpenAI, Silicon, etc.) with string content that triggered the assertion.

Changes

Production code

  • sdk/nexent/core/agents/agent_model.py: All 8 to_messages() methods now return list-format content via new _text_message() static helper. estimate_tokens() rewritten to handle list content.
  • sdk/nexent/core/agents/agent_context.py: prepare_run_context uses new _extract_message_text() for stable_text assembly. _purpose_messages returns list-format content.

Tests

  • test/sdk/core/agents/test_context_component.py: 1 assertion updated
  • test/backend/utils/test_context_component_types.py: 6 assertions updated
  • test/sdk/core/agents/test_context_manager_assembly.py: _message_text helper + _Step stub + 2 assertions updated

Verification

All 521 directly related and adjacent tests pass:

  • test_context_component.py: 68/68
  • test_context_component_types.py: 38/38
  • test_context_manager_assembly.py: 4/4
  • test_agent_context/: 126/126
  • test_agent_model.py: 224/224
  • test_openai_llm.py: 53/53
  • test_message_utils.py: 8/8
img_v3_02134_13b444fd-2bfd-4d3d-abe2-8efd9391230g

…t content

Root cause: ContextComponent subclasses returned {"content": "string"} but
smolagents v1.23 requires {"content": [{"type": "text", "text": "..."}]}
when merging consecutive same-role messages in get_clean_message_list().

This caused 'Error: wrong content: <system prompt text>' assertion failures
for all non-modelengine model providers when enable_context_manager was active.

Changes:
- agent_model.py: All 8 to_messages() methods now return list-format content
  via new _text_message() helper; estimate_tokens() handles list content
- agent_context.py: prepare_run_context uses _extract_message_text() for
  stable_text assembly; _purpose_messages returns list-format content
- Tests updated to match new content format
Copilot AI review requested due to automatic review settings June 29, 2026 10:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…essage_text branches

- estimate_tokens: test string content fallback path (else branch)
- _extract_message_text: test ChatMessage object input, string content,
  None content, missing key, and mixed list content
@Dallas98 Dallas98 merged commit 7958adc into develop Jun 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants