Skip to content

fix(agent): prevent JSON serialization errors with non-serializable direct tool parameters #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

cagataycali
Copy link
Member

Description

This PR fixes a critical issue where passing non-serializable objects as tool parameters would cause JSON serialization errors during tool call recording, leading to crashes when agents tried to record their tool usage.

The Problem

When agents passed complex objects like Agent instances, custom classes, functions, or other non-JSON-serializable objects as tool parameters, the _record_tool_execution() method would fail with cryptic JSON serialization errors, breaking the entire tool execution flow.

The Solution

  • Smart Parameter Filtering: Added logic to test each parameter with json.dumps() before recording
  • Graceful Degradation: Replace non-serializable objects with descriptive strings like <non-serializable: TypeName>
  • Maintain Functionality: Tool execution continues normally while providing clear indicators in the recorded history
  • Clean Implementation: No breaking changes to existing functionality

Key Changes

  • Add parameter filtering in _record_tool_execution() to handle non-serializable objects
  • Remove unused messages parameter from method signature
  • Fix message format consistency (agent.tool.{name} vs agent.{name})
  • Comprehensive test coverage for edge cases and regression prevention

Related Issues

Closes #350

Documentation PR

No documentation changes needed - this is an internal bug fix that maintains existing API.

Type of Change

Bug fix

Testing

Comprehensive test suite added covering:

  • Single non-serializable objects (Agent instances)
  • Multiple types of non-serializable objects (custom classes, functions, sets, complex numbers)
  • Edge cases with nested structures and None values
  • Normal serializable parameters (regression testing)
  • Disabled recording scenarios
  • I ran hatch run prepare - all pre-commit hooks passed

Test Results

All existing tests pass, and 5 new test functions added:

  • test_agent_tool_non_serializable_parameter_filtering
  • test_agent_tool_multiple_non_serializable_types
  • test_agent_tool_serialization_edge_cases
  • test_agent_tool_no_non_serializable_parameters
  • test_agent_tool_record_direct_tool_call_disabled_with_non_serializable

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly (none needed for internal bug fix)
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published (none required)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

…ool parameters

Fixes issue where passing non-serializable objects (Agent instances, custom
classes, functions, etc.) as tool parameters would cause JSON serialization
errors during tool call recording.

Changes:
- Add parameter filtering in _record_tool_execution() to test each parameter
  with json.dumps() and replace non-serializable objects with descriptive
  strings like '<non-serializable: TypeName>'
- Remove unused 'messages' parameter from _record_tool_execution method
- Fix message format consistency (agent.tool.{name} vs agent.{name})
- Add comprehensive test coverage for various non-serializable object types
  including Agent instances, custom classes, functions, sets, complex numbers
- Add edge case testing for nested structures and None values
- Add regression testing for normal serializable parameters
- Add testing for disabled recording scenarios

The fix maintains full functionality while preventing crashes, providing
clear error indicators instead of cryptic JSON serialization errors.

Closes strands-agents#350
@cagataycali cagataycali added the bug Something isn't working label Jul 18, 2025
- Replace manual serialization loop with json.dumps default parameter
- Use __qualname__ for better type representation
- Change format from <non-serializable> to <<non-serializable>>
- Update tests to match new serialization format
- Addresses PR feedback to simplify serialization approach
@cagataycali cagataycali merged commit 9faadbf into strands-agents:main Jul 18, 2025
18 of 22 checks passed
@cagataycali cagataycali deleted the fix/non-serializable-tool-parameters branch July 18, 2025 18:00
dbschmigelski pushed a commit to Ketansuhaas/sdk-python that referenced this pull request Jul 24, 2025
…irect tool parameters (strands-agents#498)

* fix(agent): prevent JSON serialization errors with non-serializable tool parameters

Fixes issue where passing non-serializable objects (Agent instances, custom
classes, functions, etc.) as tool parameters would cause JSON serialization
errors during tool call recording.

Changes:
- Add parameter filtering in _record_tool_execution() to test each parameter
  with json.dumps() and replace non-serializable objects with descriptive
  strings like '<non-serializable: TypeName>'
- Remove unused 'messages' parameter from _record_tool_execution method
- Fix message format consistency (agent.tool.{name} vs agent.{name})
- Add comprehensive test coverage for various non-serializable object types
  including Agent instances, custom classes, functions, sets, complex numbers
- Add edge case testing for nested structures and None values
- Add regression testing for normal serializable parameters
- Add testing for disabled recording scenarios

The fix maintains full functionality while preventing crashes, providing
clear error indicators instead of cryptic JSON serialization errors.

Closes strands-agents#350

* refactor: use json.dumps default parameter for non-serializable objects

- Replace manual serialization loop with json.dumps default parameter
- Use __qualname__ for better type representation
- Change format from <non-serializable> to <<non-serializable>>
- Update tests to match new serialization format
- Addresses PR feedback to simplify serialization approach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Agent object not JSON serializable when passed to direct tool calls
4 participants