Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

Add message_id to LLM events to differentiate stream chunks

Summary

This PR fixes issue #3845 by adding a unique message_id field to all LLM events. Previously, when an agent made multiple LLM calls within the same task, all LLMStreamChunkEvent chunks shared the same task_id and agent_id, making it impossible to differentiate which chunks belonged to which LLM call.

Changes:

  • Added message_id: str | None = None field to LLMEventBase (maintains backward compatibility)
  • Generate unique message_id (UUID) at the start of each LLM.call()
  • Thread message_id through all code paths (streaming, non-streaming, tool calls, error handling)
  • All LLM events now include the same message_id for events from the same call
  • Added comprehensive test suite to verify message_id uniqueness and consistency

Review & Testing Checklist for Human

⚠️ Risk Level: YELLOW - Local testing was blocked by environment issues

  • Run tests locally: Tests were written but not run locally due to uv.lock corruption. Please run uv run pytest lib/crewai/tests/test_llm_message_id.py -vv to verify they pass
  • Verify streaming behavior: Make actual streaming LLM calls from the same agent/task and confirm that events from different calls have different message_id values
  • Check event consistency: Verify that all events (Started, StreamChunk, Completed) from a single LLM call share the same message_id
  • Backward compatibility: Ensure existing event listeners that don't expect message_id still work correctly
  • Code review: Scan through the diff to ensure all function signatures were updated consistently and no call sites were missed

Test Plan

  1. Create an agent and task
  2. Make multiple LLM calls with streaming enabled
  3. Register event listeners for LLMCallStartedEvent and LLMStreamChunkEvent
  4. Verify that each call gets a unique message_id
  5. Verify that all chunks within one call share the same message_id

Notes

- Add message_id field to LLMEventBase for all LLM events
- Generate unique message_id (UUID) for each LLM call in LLM.call()
- Thread message_id through all streaming and non-streaming paths
- Update all event emissions to include message_id
- Add comprehensive tests verifying message_id uniqueness

Fixes #3845

Co-Authored-By: João <joao@crewai.com>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

2 participants