Skip to content

fix: update_instructions() now reflected in tool call response generation#5072

Open
weiguangli-io wants to merge 1 commit intolivekit:mainfrom
weiguangli-io:fix/update-instructions-in-tool-4242
Open

fix: update_instructions() now reflected in tool call response generation#5072
weiguangli-io wants to merge 1 commit intolivekit:mainfrom
weiguangli-io:fix/update-instructions-in-tool-4242

Conversation

@weiguangli-io
Copy link

Summary

  • Fixes update_instructions() not reflected in tool call response generation #4242: when update_instructions() is called inside a function tool, the updated instructions are now used for the subsequent tool response generation
  • Before this fix, the chat context was snapshotted before tool execution, so any instruction updates made during tool execution were lost for the current turn's response
  • The fix refreshes the instructions in chat_ctx from agent._instructions right before generating the tool response

Context

Maintainer @longcw confirmed this is unexpected behavior:

"it makes sense to add a update_instructions for this turn in a function tool"
"no, it's not expected"

Changes

In agent_activity.py, added a call to update_instructions() to refresh the chat context's instructions from the agent's current _instructions before the recursive _pipeline_reply_task call that generates the tool response. This ensures that any update_instructions() calls made inside function tools are reflected in the LLM's response generation for the current turn.

Test plan

  • Verify that calling agent.update_instructions() inside a @function_tool updates the instructions used for the current turn's response
  • Verify that normal (non-tool) instruction updates continue to work as before
  • Verify that instructions persist correctly across multiple tool call steps

🤖 Generated with Claude Code

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>
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

View 3 additional findings in Devin Review.

Open in Devin Review

Comment on lines +2278 to +2282
update_instructions(
chat_ctx,
instructions=self._agent._instructions,
add_if_missing=False,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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")
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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.

update_instructions() not reflected in tool call response generation

1 participant