fix: update_instructions() now reflected in tool call response generation#5072
Open
weiguangli-io wants to merge 1 commit intolivekit:mainfrom
Open
fix: update_instructions() now reflected in tool call response generation#5072weiguangli-io wants to merge 1 commit intolivekit:mainfrom
weiguangli-io wants to merge 1 commit intolivekit:mainfrom
Conversation
When update_instructions() is called inside a function tool, the updated instructions were not reflected in the subsequent tool response generation because the chat context was snapshotted before tool execution. This fix refreshes the instructions in the chat_ctx from the agent's current instructions before generating the tool response. Confirmed as unexpected behavior by maintainer @longcw. Fixes livekit#4242 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment on lines
+2278
to
+2282
| update_instructions( | ||
| chat_ctx, | ||
| instructions=self._agent._instructions, | ||
| add_if_missing=False, | ||
| ) |
Contributor
There was a problem hiding this comment.
🟡 Missing try/except ValueError around update_instructions unlike all other call sites
The new update_instructions() call is not wrapped in a try/except ValueError, unlike every other call site in the same file (agent_activity.py:600-606 and agent_activity.py:1960-1964). The update_instructions function in generation.py:820-823 explicitly raises ValueError if the existing instructions item is not of type "message". If this exception fires, it will propagate uncaught and crash the _pipeline_reply_task_impl coroutine, potentially leaving the agent in a broken state (e.g., stuck in "thinking" without transitioning back to "listening").
Suggested change
| update_instructions( | |
| chat_ctx, | |
| instructions=self._agent._instructions, | |
| add_if_missing=False, | |
| ) | |
| try: | |
| update_instructions( | |
| chat_ctx, | |
| instructions=self._agent._instructions, | |
| add_if_missing=False, | |
| ) | |
| except ValueError: | |
| logger.exception("failed to update the instructions") |
Was this helpful? React with 👍 or 👎 to provide feedback.
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
update_instructions()is called inside a function tool, the updated instructions are now used for the subsequent tool response generationchat_ctxfromagent._instructionsright before generating the tool responseContext
Maintainer @longcw confirmed this is unexpected behavior:
Changes
In
agent_activity.py, added a call toupdate_instructions()to refresh the chat context's instructions from the agent's current_instructionsbefore the recursive_pipeline_reply_taskcall that generates the tool response. This ensures that anyupdate_instructions()calls made inside function tools are reflected in the LLM's response generation for the current turn.Test plan
agent.update_instructions()inside a@function_toolupdates the instructions used for the current turn's response🤖 Generated with Claude Code