Skip to content

fix(agent): safely serialize trace payloads in capture_turn - #587

Open
krishna28004 wants to merge 5 commits into
MemoriLabs:mainfrom
krishna28004:fix-agent-trace-serialization
Open

fix(agent): safely serialize trace payloads in capture_turn#587
krishna28004 wants to merge 5 commits into
MemoriLabs:mainfrom
krishna28004:fix-agent-trace-serialization

Conversation

@krishna28004

Copy link
Copy Markdown
Contributor

What does this PR do?

Related issue

Before opening this PR

  • I have checked that there is not already an open PR for this change.
  • I have checked existing issues and discussions for relevant context.
  • I have read the contributing guidelines.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Test update
  • Refactor or maintenance
  • Performance improvement
  • Build, CI, or release change

Affected areas

  • Python SDK (memori/)
  • TypeScript SDK (memori-ts/)
  • Rust core or native bindings (core/)
  • LLM providers or adapters
  • Storage adapters or drivers
  • Memory augmentation or recall
  • Examples or integrations
  • Documentation
  • CI, packaging, or tooling

How was this tested?

  • uv run pytest
  • uv run ruff check .
  • uv run ruff format --check .
  • npm test from memori-ts/
  • npm run lint from memori-ts/
  • Integration tests
  • Manual testing

Checklist

  • I have kept this change focused and consistent with the existing architecture.
  • I have added or updated tests where appropriate.
  • I have updated documentation or examples for user-facing changes.
  • This PR does not require live API keys for unit tests.
  • This PR does not include generated artifacts, local databases, or cache files.
  • I have called out any breaking changes, migration steps, or compatibility concerns below.

Notes for reviewers

@devwdave

Copy link
Copy Markdown
Contributor

Thanks for the fix. The datetime/Pydantic/custom-object handling is a good direction, but I think this still needs one more pass before approval.

Agent.capture_turn() now runs trace through convert_to_json(), but convert_to_json() can still return unsupported objects unchanged. For example, a trace containing a uuid.UUID remains a UUID after conversion, and the required Api.post(..., json=payload) call can still fail during JSON encoding before the turn is captured.

Could we make the trace normalization fully JSON-safe, or at least add handling/tests for common non-JSON scalar types and assert that the final turn/augmentation payload can pass json.dumps(...)?

I ran:

  • uv run pytest tests/test_agent.py tests/llm/helpers/test_serialization.py
  • uv run ruff check memori/agent.py memori/llm/helpers/serialization.py tests/test_agent.py tests/llm/helpers/test_serialization.py

@krishna28004

Copy link
Copy Markdown
Contributor Author

Thanks @devwdave for catching that edge case.

I updated the PR to explicitly handle common non-JSON scalar types that could still leak through convert_to_json, including UUID, Decimal, Path, and Enum values. I also added regression coverage that verifies the normalized payload can successfully pass json.dumps(...).

The follow-up changes have been pushed in commit 0d17f2b.

Please let me know if you see any other gaps in the serialization path.

@krishna28004

Copy link
Copy Markdown
Contributor Author

Hi @devwdave,
The requested changes have been addressed and all CI checks are passing. I'd appreciate another review whenever you have time. Thanks!

Copilot AI review requested due to automatic review settings August 2, 2026 20:25

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.

Pull request overview

This PR improves robustness of agent turn capture by ensuring trace payloads are JSON-safe before being sent to Memori Cloud, preventing ingestion failures when traces contain common non-JSON Python objects.

Changes:

  • Serialize trace in Agent.capture_turn via convert_to_json before building/posting payloads.
  • Extend convert_to_json to normalize additional common types (e.g., datetime/date, Enum, UUID, Path, Decimal, and set/tuple-like iterables).
  • Add tests covering safe serialization for complex traces and the newly supported scalar/container types.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
memori/agent.py Converts trace through convert_to_json inside capture_turn before posting payloads.
memori/llm/helpers/serialization.py Adds concrete serialization branches for more non-JSON-native Python types.
tests/test_agent.py Adds an end-to-end test asserting complex trace objects are safely serialized in turn/augmentation payloads.
tests/llm/helpers/test_serialization.py Adds unit tests asserting convert_to_json handles datetime/date and several scalar/container types safely.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# 2. Assert specific structure preservation
assert result["id"] == "12345678-1234-5678-1234-567812345678"
assert result["cost"] == "1.23"
# Note: Path separator might differ on Windows/Linux, so check ends_with or just type
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