With the new agents 1.x framework, everytime the a...
# ask-ai
c
With the new agents 1.x framework, everytime the agent invokes a function call and then afterwards, the user speaks I recurringly get this error: livekit.agents._exceptions.APIStatusError: Error code: 400 - {‘error’: {‘message’: “Invalid parameter: messages with role ‘tool’ must be a response to a preceeding message with ‘tool_calls’.“, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages.[10].role’, ‘code’: None}} (status_code=400, request_id=5d182168-576f-477a-a02a-61cf4dfc143f, body={‘message’: “Invalid parameter: messages with role ‘tool’ must be a response to a preceeding message with ‘tool_calls’.“, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages.[10].role’, ‘code’: None}, retryable=True) Is there something I can do to fix this? This seems deep within the livekit framework.
t
Thanks for reaching out, Sai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error you're encountering,
APIStatusError 400
, with the message "Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls'" occurs when the conversation flow between tool calls and tool responses is not properly structured in the LiveKit agents framework. Steps to Resolve: 1. Ensure Proper Message Sequencing: Verify that your conversation flow follows this pattern: ◦ An assistant message with
tool_calls
◦ Followed by tool response messages with role 'tool' and matching
tool_call_id
1. Verify Tool Call ID Matching: Ensure the
tool_call_id
in your tool response exactly matches the
call_id
from the corresponding function call. 2. Use Built-in Validation: Utilize the framework's
group_tool_calls
function to automatically group related messages, function calls, and outputs together. This function also removes invalid tool calls and outputs. 3. Check Your ChatContext Structure: Ensure your
ChatContext
contains properly structured items where: ◦
FunctionCall
items have valid
call_id
values ◦
FunctionCallOutput
items reference the correct
call_id
from their corresponding function calls 1. Review Message Building Logic: If you're manually constructing messages, ensure that: ◦ Tool response messages have the role 'tool' ◦ They include the correct
tool_call_id
matching a previous tool call ◦ Tool responses are only sent after assistant messages containing
tool_calls
The framework's built-in validation and grouping mechanisms should handle most cases automatically, but this error typically indicates that the conversation flow has been disrupted or tool call IDs are mismatched. Sources: livekit-agents/livekit/agents/llm/_provider_format/aws.py
c
This didnt answer my question.. all Im doign is using the livekits 1.x framework.. there is no custom code on my end
@refined-appointment-81829 Can you please help?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!