Skip to content

openai.BadRequestError: Error code: 400 - Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls' #1

@supernitin

Description

@supernitin

I am encountering an error when sending subsequent requests to task_maistro. The first one is processed properly. However, the 2nd sporadicly throws this error. It happens about 80% of the time:

openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid parameter: messages with role 'tool' must be a response to a preceding message with 'tool_calls'.", 'type': 'invalid_request_error', 'param': 'messages.[5].role', 'code': None}}

Expected Behavior:
The tool calls should be correctly sequenced, and the API should process the messages without errors.

Actual Behavior:
The API returns a 400 error indicating that messages with the role 'tool' must be a response to a preceding message with 'tool_calls'.

Additional Context:

  • The error seems to be related to the sequencing of messages in the graph.
  • The traceback indicates the error occurs during the execution of the task_mAIstro function.
  • I have attempted to ensure correct sequencing and error handling, but the issue persists.
  • Any insights or suggestions for resolving this error would be greatly appreciated.
  • I'm using Azure OpenAI GPT-4o

Environment:

Operating System: macOS
Python version: 3.11.11
Langgraph version: 0.2.60
Langchain Core Version: 0.3.29
Langchain Version: 0.3.14
Platform: Linux-6.12.5-orbstack-00287-gf8da5d508983-aarch64-with-glibc2.36
Runtime: Python
SDK: langsmith-py 0.2.10
Run Metadata:

Assistant ID: ea4ebafa-a81d-5063-a5fa-67c755d98a21
Created By: system
Graph ID: task_maistro
Langgraph Host: self-hosted
Langgraph Plan: developer
Langgraph Version: 0.2.60
LANGSMITH_LANGGRAPH_API_REVISION: ed92a0f
LANGSMITH_LANGGRAPH_API_VARIANT: local
Revision ID: e7586c9
Run Attempt: 1
Run ID: 1efce944-fd32-6cdc-94de-929c8772430f
Thread ID: b6f30a04-e4f1-4dc1-9ec5-46a6042ff917
X-Middleware-Subrequest: /api/[..._path]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions