Interruptions during function execution can cause tool_result message to be orphaned causing agent to fall silent.
First off, love what you're doing. I am struggling to fix the following issue, which I can recreate consistently.
When a couple of function executions are interrupted in relatively quick succession this causes the agent to fall silent:
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.0: tool_result block(s) provided when previous message does not contain any tool_use blocks'}} {"pid": 18061, "job_id": "AJ_DM5he9c6Fvki"}
I believe this is because the message history gets corrupted causing the tool blocks to be decoupled.
Thanks for any help in advance :)
i got this error too occasionally, please fix it
similar error. see the trace below:
ERROR asyncio - _GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=CancelledError()>
Traceback (most recent call last):
File ".../lib/python3.12/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs
return await fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/plugins/deepgram/tts.py", line 280, in _run_segments
async for word in word_stream:
File ".../lib/python3.12/site-packages/livekit/agents/tokenize/token_stream.py", line 108, in anext
return await self._event_ch.anext()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/channel.py", line 173, in anext
return await self.recv()
^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/channel.py", line 103, in recv
await g
asyncio.exceptions.CancelledError {"pid": 14266, "job_id": "AJ_8EUcctsnF2d7"}
2025-01-28 14:51:04,121 - ERROR livekit.agents.pipeline - Error in _stream_synthesis_task
Traceback (most recent call last):
File ".../lib/python3.12/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs
return await fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/agents/pipeline/agent_output.py", line 273, in _stream_synthesis_task
async for seg in tts_source:
File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/itertools.py", line 47, in tee_peer
item = await iterator.anext()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/agents/pipeline/pipeline_agent.py", line 1063, in _llm_stream_to_str_generator
async for chunk in stream:
File ".../lib/python3.12/site-packages/livekit/agents/llm/llm.py", line 239, in anext
raise exc from None
File ".../lib/python3.12/site-packages/livekit/agents/llm/llm.py", line 149, in _main_task
return await self._run()
^^^^^^^^^^^^^^^^^
File ".../lib/python3.12/site-packages/livekit/plugins/anthropic/llm.py", line 234, in _run
raise APIStatusError(
livekit.agents._exceptions.APIStatusError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'Requests which include tool_use or tool_result blocks must define tools.'}} {"pid": 14266, "job_id": "AJ_8EUcctsnF2d7"}
Possible Solution
To fix the HTTP 400 Bad Request error with the message "Requests which include tool_use or tool_result blocks must define tools," you need to ensure the following:
Define Tools: Include the necessary tool definitions in your API request.
Correct JSON Structure: Ensure the tools are specified in the correct JSON format, including all required fields such as name, description, and input schema.
Tool Configuration: Verify that each tool is correctly configured and transformation methods are applied as needed.
Prompt Message Formatting: Make sure the prompt message dictionaries are correctly formatted and include the necessary tool_use or tool_result blocks.
By addressing these aspects, you can resolve the issue and successfully use tools with the Anthropic API.
Yeah this happens because livekit just sets fnc_ctx = None when they dont want the LLM to not call a tool instead of setting tool_choice
@jenfic do you happen to have a reproduction for this?
@eshamanideep do you mind elaborating on what the expected behavior would be?
Removing fnc_ctx is fine if its an OpenAI LLM we are making the request to - since anthropic fails if you remove fnc_ctx (anthropic fails if tool calls in chat_ctx, tools = []) three ways you can go about this:
- Check whether the provider is anthropic or not and set fnc_ctx = None based on that in pipeline_agent.py
- In anthropic/llm.py cache the old fnc_ctx and send that to the LLM based on whether fnc_ctx is None or not
- Remove support for max_nested_fnc_calls for anthropic LLMs and let users take care of it (seems the best imo)
Anthropic doesn't also support tool_choice = "none" so can't really do much here other than removing support for anthropic
In our current pipeline, we just set it to 10 or something if its anthropic LLM
hey folks, do you have a way to reproduce this issue? is it just using Anthropic with function calls?
Yeah this happens because livekit just sets fnc_ctx = None when they dont want the LLM to not call a tool instead of setting tool_choice
Whenever functions are defined, they are always sent to the LLM. code
similar error. see the trace below:
ERROR asyncio - _GatheringFuture exception was never retrieved future: <_GatheringFuture finished exception=CancelledError()> Traceback (most recent call last): File ".../lib/python3.12/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/plugins/deepgram/tts.py", line 280, in _run_segments async for word in word_stream: File ".../lib/python3.12/site-packages/livekit/agents/tokenize/token_stream.py", line 108, in anext return await self._event_ch.anext() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/channel.py", line 173, in anext return await self.recv() ^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/channel.py", line 103, in recv await g asyncio.exceptions.CancelledError {"pid": 14266, "job_id": "AJ_8EUcctsnF2d7"} 2025-01-28 14:51:04,121 - ERROR livekit.agents.pipeline - Error in _stream_synthesis_task Traceback (most recent call last): File ".../lib/python3.12/site-packages/livekit/agents/utils/log.py", line 16, in async_fn_logs return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/agents/pipeline/agent_output.py", line 273, in _stream_synthesis_task async for seg in tts_source: File ".../lib/python3.12/site-packages/livekit/agents/utils/aio/itertools.py", line 47, in tee_peer item = await iterator.anext() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/agents/pipeline/pipeline_agent.py", line 1063, in _llm_stream_to_str_generator async for chunk in stream: File ".../lib/python3.12/site-packages/livekit/agents/llm/llm.py", line 239, in anext raise exc from None File ".../lib/python3.12/site-packages/livekit/agents/llm/llm.py", line 149, in _main_task return await self._run() ^^^^^^^^^^^^^^^^^ File ".../lib/python3.12/site-packages/livekit/plugins/anthropic/llm.py", line 234, in _run raise APIStatusError( livekit.agents._exceptions.APIStatusError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'Requests which include
tool_useortool_resultblocks must define tools.'}} {"pid": 14266, "job_id": "AJ_8EUcctsnF2d7"}
original issue might not be a model specific, my guess is when interuption during function call happens, somehow fnc_ctx is becoming None and that's causing the issue, checking this further
Livekit sets fnc_ctx = None when max_nested_fnc_calls is reached here. Since this is set after some tool calls by anthropic, it fails because anthropic's API doesn't allow tools = [] when there are tool calls in conversation history.
Best solution here is to add something on comments/docs to let users know max_nested_fnc_calls doesnt work as expected on anthropic and let users figure out what they wanna do
hey folks, do you have a way to reproduce this issue? is it just using Anthropic with function calls?
Yes with function calls. This issue is also seen when using the aws llm plugin as well. Albeit, aws plugin is not yet merged into main.