Skip to content

fix(llm): use validated messages variable instead of raw params['messages'] access#5166

Closed
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1774872223-fix-keyerror-params-messages
Closed

fix(llm): use validated messages variable instead of raw params['messages'] access#5166
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1774872223-fix-keyerror-params-messages

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot commented Mar 30, 2026

Summary

Fixes #5164. Replaces all direct params["messages"] accesses in _handle_emit_call_events calls with safe alternatives across three methods:

  • _handle_non_streaming_response and _ahandle_non_streaming_response: In the response_model + is_litellm early-return branch, uses the already-extracted local messages variable (from params.get("messages", [])) instead of re-accessing params["messages"]. In all other branches, uses params.get("messages", []).
  • _handle_streaming_response: Uses params.get("messages", []) for all emit calls (this method had no local messages variable).

12 occurrences of params["messages"] replaced total. _ahandle_streaming_response was already using params.get("messages") and was not changed.

Review & Testing Checklist for Human

  • Verify the two uses of the bare messages local variable (lines ~1220 and ~1364) are actually in scope — they should be, since messages = params.get("messages", []) is assigned earlier in the same if response_model and self.is_litellm: block
  • Confirm no behavioral regression for the normal path where params["messages"] does exist — params.get("messages", []) returns the same value as params["messages"] when the key is present
  • Note: _ahandle_streaming_response uses params.get("messages") (returns None if missing) while the patched methods use params.get("messages", []) (returns []). Both are acceptable for _handle_emit_call_events which accepts None, but the inconsistency is worth being aware of

Notes

  • In normal usage via call()/acall(), params["messages"] is always populated before these handlers are invoked. This fix is defensive — it prevents a confusing KeyError if the internal methods are ever called with incomplete params.
  • 5 new unit tests added covering both sync and async non-streaming paths with and without response_model.

Link to Devin session: https://app.devin.ai/sessions/f9fa64e3c2b346e2b332c8b2e21c5512


Note

Low Risk
Low risk defensive change: swaps direct params["messages"] access for params.get(...)/validated messages, affecting only event emission metadata and adding tests to prevent regressions.

Overview
Prevents KeyError when internal LLM response handlers emit LLMCallCompletedEvent without a messages key in params (fix for #5164).

All _handle_emit_call_events call sites in streaming and (a)sync non-streaming paths now pass params.get("messages", []), and the LiteLLM response_model early-return branches pass the already-validated local messages variable. Adds unit tests covering sync/async non-streaming behavior with missing messages and with response_model to ensure proper errors (ValueError) and no KeyError.

Written by Cursor Bugbot for commit 25a13a0. This will update automatically on new commits. Configure here.

…ages'] access

Replace all direct params['messages'] accesses with params.get('messages', [])
or the locally validated 'messages' variable in _handle_non_streaming_response,
_ahandle_non_streaming_response, and _handle_streaming_response.

When the 'messages' key is missing from params, the code now gracefully handles
it instead of raising a confusing KeyError.

Closes #5164

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

Prompt hidden (unlisted session)

@devin-ai-integration
Copy link
Copy Markdown
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

@greysonlalonde
Copy link
Copy Markdown
Contributor

Duplicate of #5165. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] KeyError when params missing 'messages' key in _handle_non_streaming_response

1 participant