Skip to content

fix: serialize tool outputs to JSON, remove trailing assistant messages, resolve Bedrock model IDs - #8144

Open
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/8014-cap-mcp-dependency
Open

MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/8014-cap-mcp-dependency

Conversation

@wasim-builds

@wasim-builds MOHAMMED WASIM KHAN (wasim-builds) commented Sep 2, 2026 •

Copy link
Copy Markdown

Summary

Fixes #7867, #7768, and #7833

#7867: Tool output serialization

BaseTool.return_value_as_string used str() for dicts/lists, producing Python repr instead of valid JSON.

#7768: Anthropic trailing assistant messages

_rstrip_last_assistant_message only stripped whitespace but didn't remove trailing assistant messages.

#7833: Bedrock model ID resolution

get_info/get_token_limit could not resolve Bedrock model IDs with provider prefixes.

Changes

  • Updated BaseTool.return_value_as_string to use json.dumps for dict/list values
  • Updated Anthropic _rstrip_last_assistant_message to remove trailing assistant messages
  • Added _normalize_model_id helper for Bedrock model ID resolution
  • Added regression tests for all fixes

Test plan

  • New tests pass
  • Existing tests still pass

Copilot AI lite review requested due to automatic review settings September 2, 2026 04:56
@wasim-builds MOHAMMED WASIM KHAN (wasim-builds) changed the title fix: serialize dict/list tool outputs to valid JSON fix: serialize dict/list tool outputs to valid JSON, remove trailing assistant messages for Anthropic Sep 2, 2026

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.

🟡 Changes recommended

json.dumps on arbitrary dict/list contents can raise TypeError, potentially breaking tool execution paths that should not fail due to result stringification/logging, and the new tests introduce strict type-checking violations via unparameterized dict/list annotations.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes structured tool return-value serialization in autogen-core so dict/list outputs are emitted as valid JSON (via json.dumps) rather than Python repr strings, reducing downstream parsing fragility in agent pipelines.

Changes:

  • Updated BaseTool.return_value_as_string to JSON-serialize dict/list return values.
  • Added regression tests asserting FunctionTool dict/list return values serialize as JSON.
  • Kept existing Pydantic BaseModel serialization behavior (already JSON-based for dict dumps).
File summaries
File Description
python/packages/autogen-core/src/autogen_core/tools/_base.py Switches dict/list stringification from str(...) to json.dumps(...) in tool result serialization.
python/packages/autogen-core/tests/test_tools.py Adds regression tests covering JSON serialization for dict/list tool outputs.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +174 to +176
if isinstance(value, (dict, list)):
return json.dumps(value)

Comment on lines +487 to +488
def my_function() -> dict:
return {"status": "success", "count": 42}
Comment on lines +501 to +502
def my_function() -> list:
return ["a", "b", "c"]
@wasim-builds MOHAMMED WASIM KHAN (wasim-builds) changed the title fix: serialize dict/list tool outputs to valid JSON, remove trailing assistant messages for Anthropic fix: serialize tool outputs to JSON, remove trailing assistant messages, resolve Bedrock model IDs Sep 2, 2026
@gitguardian

gitguardian Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
While these secrets were previously flagged, we no longer have a reference to the
specific commits where they were detected. Once a secret has been leaked into a git
repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

@wasim-builds

Copy link
Copy Markdown
Author

I've handled the TypeError fallback for json.dumps in BaseTool.return_value_as_string, parameterized the test annotations to fix the typing violations (dict[str, Any], list[str]), and added a # ggignore to the Bedrock model IDs in the Anthropic client tests to silence the false positive from GitGuardian.

@wasim-builds

Copy link
Copy Markdown
Author

Hello maintainers,

Checking in on this pull request (fix: serialize tool outputs to JSON, remove trailing assistant messages, resolve Bedrock model IDs) for microsoft/autogen. Contributing around multi-agent orchestration, agent communication, and tool execution in AutoGen has been exciting, and I'd love to help out with additional improvements and features across microsoft/autogen.

I'm available for both freelance/contract work and full-time software engineering roles, or joining the project as an active contributor.

Feel free to check out my profile and other work at https://github.com/wasim-builds.

This branch has not been deployed

No deployments
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.

bug: ToolCallResult auto-coerces structured tool outputs to str(), losing type information

2 participants