fix(llm): use validated messages variable instead of raw params['messages'] access#5166
Closed
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Closed
fix(llm): use validated messages variable instead of raw params['messages'] access#5166devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
…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>
Contributor
Author
|
Prompt hidden (unlisted session) |
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Contributor
|
Duplicate of #5165. Closing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5164. Replaces all direct
params["messages"]accesses in_handle_emit_call_eventscalls with safe alternatives across three methods:_handle_non_streaming_responseand_ahandle_non_streaming_response: In theresponse_model + is_litellmearly-return branch, uses the already-extracted localmessagesvariable (fromparams.get("messages", [])) instead of re-accessingparams["messages"]. In all other branches, usesparams.get("messages", [])._handle_streaming_response: Usesparams.get("messages", [])for all emit calls (this method had no localmessagesvariable).12 occurrences of
params["messages"]replaced total._ahandle_streaming_responsewas already usingparams.get("messages")and was not changed.Review & Testing Checklist for Human
messageslocal variable (lines ~1220 and ~1364) are actually in scope — they should be, sincemessages = params.get("messages", [])is assigned earlier in the sameif response_model and self.is_litellm:blockparams["messages"]does exist —params.get("messages", [])returns the same value asparams["messages"]when the key is present_ahandle_streaming_responseusesparams.get("messages")(returnsNoneif missing) while the patched methods useparams.get("messages", [])(returns[]). Both are acceptable for_handle_emit_call_eventswhich acceptsNone, but the inconsistency is worth being aware ofNotes
call()/acall(),params["messages"]is always populated before these handlers are invoked. This fix is defensive — it prevents a confusingKeyErrorif the internal methods are ever called with incomplete params.response_model.Link to Devin session: https://app.devin.ai/sessions/f9fa64e3c2b346e2b332c8b2e21c5512
Note
Low Risk
Low risk defensive change: swaps direct
params["messages"]access forparams.get(...)/validatedmessages, affecting only event emission metadata and adding tests to prevent regressions.Overview
Prevents
KeyErrorwhen internal LLM response handlers emitLLMCallCompletedEventwithout amessageskey inparams(fix for #5164).All
_handle_emit_call_eventscall sites in streaming and (a)sync non-streaming paths now passparams.get("messages", []), and the LiteLLMresponse_modelearly-return branches pass the already-validated localmessagesvariable. Adds unit tests covering sync/async non-streaming behavior with missingmessagesand withresponse_modelto ensure proper errors (ValueError) and noKeyError.Written by Cursor Bugbot for commit 25a13a0. This will update automatically on new commits. Configure here.