Skip to content

feat: make ToolCall serializable for message history persistence#444

Open
majiayu000 wants to merge 1 commit intoMadcowD:mainfrom
majiayu000:fix/issue-412-toolcall-serializable
Open

feat: make ToolCall serializable for message history persistence#444
majiayu000 wants to merge 1 commit intoMadcowD:mainfrom
majiayu000:fix/issue-412-toolcall-serializable

Conversation

@majiayu000
Copy link

Summary

  • Added field_serializer and field_validator for ToolCall.tool to enable JSON serialization
  • Added field_serializer for ToolCall.params with type info for reconstruction
  • Fixed _lstr origin_trace deserialization to handle both list and string formats

Changes

  • src/ell/types/message.py: Added serializers/validators for ToolCall
  • src/ell/types/_lstr.py: Fixed origin_trace deserialization
  • tests/test_tool_call_serialization.py: Added 9 comprehensive tests

Use Case

This enables workflow persistence where message history with pending tool calls can be saved and restored:

# Save message with tool calls
message = Message(role="assistant", content=[ToolCall(...)])
json_str = message.model_dump_json()
save_to_db(json_str)

# Later, restore and continue
loaded = Message.model_validate_json(json_str)
result = loaded.call_tools_and_collect_as_message()

Test plan

  • All existing tests pass (110 passed)
  • Added 9 new tests for ToolCall serialization
  • Verified roundtrip serialization/deserialization
  • Verified tool invocation after deserialization

Fixes #412

Added field_serializer and field_validator for ToolCall.tool to enable
JSON serialization/deserialization of Messages containing ToolCalls.

Changes:
- ToolCall.tool: serialize to {module, qualname}, resolve on deserialize
- ToolCall.params: serialize with type info for proper reconstruction
- _lstr: fix origin_trace deserialization to handle both list and string

This enables workflow persistence where message history with pending
tool calls can be saved and restored across process restarts.

Fixes MadcowD#412

Signed-off-by: majiayu000 <1835304752@qq.com>
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.

ToolCall should be serializable

1 participant