Repository navigation
fix(prompt_templates): do not mutate messages in-place in function_call_prompt - #41676
Open
mgeorgaklis wants to merge 5 commits into
Open
mgeorgaklis wants to merge 5 commits into
mgeorgaklis wants to merge 5 commits into
Conversation
Contributor
|
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
4 of 6 tasks
This branch has not been deployed
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
TLDR: Shallow copies in message validation cause token inflation through duplicate tool definitions across multi-turn conversations
function_call_prompt(messages, functions)inlitellm/litellm_core_utils/prompt_templates/factory.pypreviously mutated the caller'smessageslist, system message dictionary, or nested content list in-placeBecause
validate_and_fix_openai_messages(messages)performs a shallow copy of message dictionaries ({**m}), nested content lists remain shared with the caller's conversation history. In multi-turn tool-calling loops that use fallback tool prompting (such as Ollama tool calling or whenadd_function_to_prompt=True), each turn appended another duplicate copy of all tool definitions to the caller's retained system message, causing Turn N to send N copies of the tool schema promptChanges
This change updates
function_call_promptto construct and return fresh message dictionaries and content lists without mutating the caller's input objects in-place. It also adds parametrized multi-turn unit tests (test_function_call_prompt_does_not_inflate_multi_turn_history) built with immutable functional reductions and TypedDict bindings to verify that caller history is preserved and tool schemas are injected once per turn