Skip to content

mistralai: emit prompt events for ToolMessage and other SDK message objects - #4524

Open
charan-rathore wants to merge 4 commits into
traceloop:mainfrom
charan-rathore:fix-4523-mistral-toolmessage-events
Open

charan-rathore wants to merge 4 commits into
traceloop:mainfrom
charan-rathore:fix-4523-mistral-toolmessage-events

Conversation

@charan-rathore

@charan-rathore charan-rathore commented Sep 28, 2026 •

Copy link
Copy Markdown

Summary

Fixes #4523. In Mistral event mode, the prompt loop recognizes user, assistant, and system SDK messages and plain dicts, but not ToolMessage. A tool result first in the list raised an internal exception and lost the prompt events; a tool result after a user prompt reused the previous message's role and content.

Read role and content from any other SDK message object, including ToolMessage, for each iteration. This emits the tool's own event and does not copy the preceding message. It also keeps future message types from inheriting stale values. The legacy span-attribute path is unchanged; open PR #4509 is working in that area.

Tests

An offline test failed before the fix for both tool-first (no prompt events) and tool-after-user (duplicate user event). It now checks each message's event name and content, plus a previously unknown message class after a user prompt.

uv run --frozen --group dev --group test pytest tests/ -q in the Mistral instrumentation package: 29 passed. Ruff and diff checks passed. No cloud key or model call was needed.

Instinct assisted with the change and tests.

Summary by CodeRabbit

  • Bug Fixes
    • Chat events now retain the correct role and content for nonstandard message formats instead of carrying over details from a previous message.
    • Tool and unrecognized message events now preserve their own content and appear in the correct order alongside user and assistant events.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 85cf5cfa-3f1c-492c-b966-9b2d7eea75af

📥 Commits

Reviewing files that changed from the base of the PR and between 6102f9e and 22b22c0.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-mistralai/opentelemetry/instrumentation/mistralai/__init__.py
  • packages/opentelemetry-instrumentation-mistralai/tests/test_tool_message_events.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The MistralAI instrumentation now reads role and content from unrecognized message objects when emitting events. Tests cover tool and user event content and ordering, and an unrecognized message with assistant role and content.

Changes

MistralAI message event handling

Layer / File(s) Summary
Emit events for unrecognized message objects
packages/opentelemetry-instrumentation-mistralai/opentelemetry/instrumentation/mistralai/__init__.py, packages/opentelemetry-instrumentation-mistralai/tests/test_tool_message_events.py
The event emitter reads role and content attributes for unrecognized message objects, defaulting to "unknown" and None. Tests check tool and user event content and order, and an unrecognized message with assistant role and content.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 22b22

The change has no identified merge-blocking issue; normal checks remain appropriate.

Security Architecture Review

Security architecture risk: 🟡 Moderate · up to 22b22

Tool results can now appear in message-event logs when event logging is active. Message-content tracing is enabled by default, so this expands the information those logs may contain. Disabling content tracing still removes message bodies.

Retained concerns

  • Medium · security · inferred: Default-enabled content tracing can now record tool-result content in message-event logs; such results may contain sensitive data that this event path previously did not record.
Security review details

Security Blast Radius

  • inferred — The added content exposure is confined to instrumented Mistral chat inputs taking the event-logging path; the evidence does not establish downstream log destinations or retention.

Security Findings and Attack Paths

  • inferred — A caller-supplied tool result, potentially containing data from another source, can now reach telemetry as message content under the default content setting. No unauthorized exporter access or control bypass is established.

Trust Boundaries and Controls

  • observed — The existing event-mode gate remains in place, and disabling content tracing strips content before the log record is emitted.

Hardening Proposals

  • proposed — For deployments where tool results require narrower handling than other prompts, consider a separate content policy or redaction before tool-message content reaches the logger.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: emitting prompt events for Mistral ToolMessage and other SDK message objects.
Linked Issues check ✅ Passed Issue [#4523] requires event-mode prompt events for ToolMessage and other SDK message types. The change reads each non-dictionary message object's own role and content, with safe defaults. The tes…
Out of Scope Changes check ✅ Passed The diff changes only Mistral event-message handling and adds focused tests for the behavior in [#4523]. No unrelated production behavior or unrelated files are changed.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

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 Report: mistralai event mode loses prompt events when a ToolMessage is present

2 participants