Skip to content

fix: convert tool result content to string for provider compatibility - #680

Open
Giggitycountless wants to merge 1 commit into
lastmile-ai:mainfrom
Giggitycountless:fix/tool-result-content-type
Open

fix: convert tool result content to string for provider compatibility#680
Giggitycountless wants to merge 1 commit into
lastmile-ai:mainfrom
Giggitycountless:fix/tool-result-content-type

Conversation

@Giggitycountless

@Giggitycountless Giggitycountless commented Apr 30, 2026

Copy link
Copy Markdown

Description

OpenAI's API accepts both string and list[ChatCompletionContentPartTextParam] formats for tool message content, but third-party providers (DeepSeek, etc.) only accept a plain string. This causes deserialization errors:

Failed to deserialize the JSON body into the target type: messages[N]: invalid type: sequence, expected a string

Root Cause

In execute_tool_call(), tool result content was built as a list:

content=[mcp_content_to_openai_content_part(c) for c in result.content]

This creates [{"type": "text", "text": "..."}] which OpenAI accepts but DeepSeek rejects.

Fix

Add _tool_result_to_string() helper that extracts text from MCP content parts and joins them into a plain string. For non-text parts (images, binary resources), includes a descriptive placeholder.

# Before
content=[mcp_content_to_openai_content_part(c) for c in result.content]

# After  
content=self._tool_result_to_string(result.content)

The helper handles all MCP content types:

  • TextContent → extracts .text
  • EmbeddedResource with TextResourceContents → extracts .resource.text
  • EmbeddedResource with BlobResourceContents[mimeType] placeholder
  • ImageContent[image: mimeType] placeholder

Fixes #25

How Has This Been Tested?

  • Verified _tool_result_to_string() correctly handles text, image, and empty content
  • Confirmed existing tests still pass (pre-existing generate_stream abstract method error is unrelated)

Checklist

  • I have read the Contributing Guide
  • My changes follow the established code style
  • I have tested my changes locally

Summary by CodeRabbit

Release Notes

  • Refactor
    • Improved tool result message formatting for language model integration, now using plain string format for more consistent handling of tool outputs and embedded content.

OpenAI's API accepts both string and list formats for tool message
content, but third-party providers (DeepSeek, etc.) only accept a
plain string, causing deserialization errors like:

  'messages[N]: invalid type: sequence, expected a string'

Add _tool_result_to_string() helper that extracts text from MCP
content parts and joins them. This replaces the list-based content
in execute_tool_call() with a provider-compatible string.

Fixes lastmile-ai#25
@coderabbitai

coderabbitai Bot commented Apr 30, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d9e8fc08-56b0-4e15-9e30-c715a9250716

📥 Commits

Reviewing files that changed from the base of the PR and between f62d849 and ad562da.

📒 Files selected for processing (1)
  • src/mcp_agent/workflows/llm/augmented_llm_openai.py

📝 Walkthrough

Walkthrough

Tool-call message construction in OpenAI augmented LLM now converts MCP result content to a plain string instead of a list. A new helper method processes TextContent, ImageContent, and EmbeddedResource parts, concatenating text and inserting placeholders for non-text items.

Changes

Cohort / File(s) Summary
Tool Result Content Formatting
src/mcp_agent/workflows/llm/augmented_llm_openai.py
Added static method _tool_result_to_string() that transforms MCP content parts into a single concatenated string for OpenAI ChatCompletionToolMessageParam, handling TextContent, ImageContent, and EmbeddedResource objects with appropriate placeholders for non-text items.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • Fix empty mcp tool result error #330: Also modifies tool-call handling for empty MCP tool results in the same file, adding logic to handle ChatCompletionToolMessageParam construction—directly related to this PR's content formatting fix.
  • Fix openai workflow for handling images #341: Adjusts tool-call content handling in augmented_llm_openai.py for image content formatting and mixed-message returns—overlaps with this PR's content transformation logic.

Suggested reviewers

  • rholinshead
  • saqadri

Poem

🐰 A rabbit's ode to string conversion:

Lists were causing trouble, causing pain,
OpenAI wanted strings, oh so plain!
Mix the content parts with care and glow,
Text concatenated, steady and slow. ✨
No more list confusion, all is right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: converting tool result content to a string for provider compatibility, which is the core fix in this PR.
Linked Issues check ✅ Passed The PR successfully addresses issue #25 by converting tool result content from a list to a plain string, preventing deserialization errors in non-OpenAI providers while maintaining compatibility.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the tool result content type issue. The addition of the _tool_result_to_string helper and its usage in execute_tool_call are within scope.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

content in OpenAI ChatCompletion messages is a list, not string

1 participant