Skip to content

fix: redact ImageContent and FileContent in ToolCallResult trace dicts - #12824

Merged
sjrl merged 4 commits into
deepset-ai:mainfrom
rautaditya2606:fix/redact-tool-call-result-traces
Oct 12, 2026
Merged

sjrl merged 4 commits into
deepset-ai:mainfrom
rautaditya2606:fix/redact-tool-call-result-traces

Conversation

@rautaditya2606

@rautaditya2606 rautaditya2606 commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Closes #12823

Proposed Changes

  • Added ToolCallResult._to_trace_dict() to redact large base64 binary payloads in ImageContent and FileContent when serialized for tracing.
  • Handled any Sequence content (lists, tuples, custom sequences) and avoided duplicate recursive serialization by constructing the trace dictionary directly.
  • Updated ChatMessage._to_trace_dict() to dispatch to ToolCallResult._to_trace_dict().
  • Added unit tests asserting placeholder replacement and verifying raw base64 data is absent from trace dictionaries (covering list and tuple sequences).
  • Added a release note describing the fix.

Root Cause

ChatMessage._to_trace_dict() redacts large binary payloads in top-level ImageContent and FileContent, but had no branch for ToolCallResult. Furthermore, ToolCallResult did not implement _to_trace_dict().

As a result, when tools returned multimodal content (such as ImageContent or FileContent), tracing spans serialized them via default to_dict(), leaking full, unredacted multi-megabyte base64 strings into telemetry backends.

Fix

  1. Implemented ToolCallResult._to_trace_dict() to inspect parts in self.result for any Sequence and redact ImageContent or FileContent objects to placeholder strings ("Base64 string (N characters)").
  2. Directly construct the trace dictionary without invoking to_dict()/asdict() to avoid duplicate recursive serialization and memory overhead.
  3. Added an explicit isinstance(part, ToolCallResult) branch in ChatMessage._to_trace_dict().

Reproducer

from haystack.dataclasses import ChatMessage, ImageContent, ToolCall

tc = ToolCall(id="call_1", tool_name="fetch_image", arguments={})
raw_base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" * 1000
image = ImageContent(base64_image=raw_base64)
message = ChatMessage.from_tool(tool_result=[image], origin=tc)

trace_dict = message._to_trace_dict()

# Leaks raw base64 data into trace dict instead of placeholder
assert raw_base64 not in str(trace_dict)

Before the fix:

raw_base64 in str(trace_dict) -> True (unredacted base64 payload leaked)

After the fix:

raw_base64 in str(trace_dict) -> False (redacted to 'Base64 string (88000 characters)')

Tests

Ran the affected test suites:

hatch -e test run pytest test/dataclasses/test_chat_message.py test/tracing/test_utils.py

Result:

128 passed in 0.35s

Also ran the newly added tests specifically:

hatch -e test run pytest \
  test/dataclasses/test_chat_message.py \
  -k "test_tool_call_result_to_trace_dict or test_to_trace_dict_with_tool_call_result"

Result:

4 passed, 98 deselected in 0.08s

Quality checks:

hatch run fmt
hatch -e test run mypy haystack/dataclasses/chat_message.py test/dataclasses/test_chat_message.py

Result:

All checks passed!
Success: no issues found in 2 source files

Checklist

  • Read contributors guidelines and code of conduct
  • Added/updated related issue reference
  • Added unit tests
  • Updated docstrings/documentation where required
  • Added release note
  • Used conventional commit type in PR title
  • Ran pre-commit hooks and fixed reported issues

Copilot AI lite review requested due to automatic review settings September 20, 2026 08:15
@rautaditya2606
rautaditya2606 requested a review from a team as a code owner September 20, 2026 08:15
@rautaditya2606
rautaditya2606 requested review from julian-risch and removed request for a team September 20, 2026 08:15
@vercel

vercel Bot commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

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.

Copilot review overview

🟡 Changes recommended

A critical sequence-handling leak and a moderate serialization-efficiency issue remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Fixes tracing leaks by redacting base64 payloads nested in ToolCallResult.

Changes:

  • Adds trace-specific redaction for image and file content.
  • Routes tool results through trace serialization.
  • Adds regression tests and a release note.
File Summary Findings
test/​dataclasses/​test_chat_message.py Adds redaction regression tests. None
releasenotes/​notes/​redact-tool-call-result-traces-03ffd820959a52da.yaml Documents the fix. None
haystack/​dataclasses/​chat_message.py Implements redaction and dispatch logic. Critical (3 votes): handle tuple and other Sequence values. Moderate (1 vote): avoid duplicate recursive serialization.

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

Comment thread haystack/dataclasses/chat_message.py Resolved
@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/dataclasses
  chat_message.py
Project Total  

This report was generated by python-coverage-comment-action

@rautaditya2606

Copy link
Copy Markdown
Contributor Author

hello @julian-risch, just giving this a gentle bump when you have a moment to review. All CI checks are passing.

I have another bug fix ready to submit (#13109), but I'm currently holding off due to the community open PR limit. Thanks!

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.

Copilot review overview

🟡 Changes recommended

Specialized trace serialization should run before generic serialization to avoid unnecessary work and peak memory.

Review effort: Lite
Findings: 1 High severity · 1 Low severity

Open (2)

Comment thread haystack/dataclasses/chat_message.py Resolved
@github-actions github-actions Bot added the P1 High priority, add to the next sprint label Oct 8, 2026
Comment thread test/dataclasses/test_chat_message.py Resolved
Comment thread test/dataclasses/test_chat_message.py Resolved
@rautaditya2606
rautaditya2606 force-pushed the fix/redact-tool-call-result-traces branch from f8e315d to 4f6c092 Compare October 9, 2026 12:48
@rautaditya2606

Copy link
Copy Markdown
Contributor Author

hello @sjrl, ive implemented both suggestions
thanks

@sjrl
sjrl requested review from sjrl and removed request for julian-risch October 12, 2026 08:39

@sjrl sjrl 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.

Thanks!

@sjrl
sjrl merged commit f569c1d into deepset-ai:main Oct 12, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High priority, add to the next sprint topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatMessage._to_trace_dict() leaks raw base64 data for ImageContent and FileContent in ToolCallResult

3 participants