Skip to content

fix(agent): fix orphaned tool-role message causing API 400 errors#153

Merged
yhjun1026 merged 1 commit intoderisk-ai:mainfrom
niiish32x:main
Mar 9, 2026
Merged

fix(agent): fix orphaned tool-role message causing API 400 errors#153
yhjun1026 merged 1 commit intoderisk-ai:mainfrom
niiish32x:main

Conversation

@niiish32x
Copy link
Contributor

Root cause: when the LLM responded with plain text (no tool_calls) in a function-calling session, the next iteration still built history pairs of
assistant(tool_calls=null) → tool(tool_call_id=...)
which violates the OpenAI-compatible API contract and produces:
"messages with role 'tool' must be a response to a preceeding message
with 'tool_calls'"

Fix (base_agent.py):

  • function_callning_reply_messages: skip appending tool-result messages when the preceding assistant message has no tool_calls, preventing the invalid sequence from being produced in the first place.
  • _sanitize_tool_messages: new helper that scrubs any remaining orphaned tool messages from the final LLM message list immediately before the API call, acting as a last-resort safety net.

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Snapshots:

Include snapshots for easier review.

Checklist:

  • My code follows the style guidelines of this project
  • I have already rebased the commits and make the commit message conform to the project standard.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • Any dependent changes have been merged and published in downstream modules

Root cause: when the LLM responded with plain text (no tool_calls) in a
function-calling session, the next iteration still built history pairs of
  assistant(tool_calls=null) → tool(tool_call_id=...)
which violates the OpenAI-compatible API contract and produces:
  "messages with role 'tool' must be a response to a preceeding message
   with 'tool_calls'"

Fix (base_agent.py):
- `function_callning_reply_messages`: skip appending tool-result messages
  when the preceding assistant message has no tool_calls, preventing the
  invalid sequence from being produced in the first place.
- `_sanitize_tool_messages`: new helper that scrubs any remaining orphaned
  tool messages from the final LLM message list immediately before the API
  call, acting as a last-resort safety net.
Copy link
Collaborator

@yhjun1026 yhjun1026 left a comment

Choose a reason for hiding this comment

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

r+

@yhjun1026 yhjun1026 merged commit 26c28bd into derisk-ai:main Mar 9, 2026
1 check passed
yhjun1026 pushed a commit that referenced this pull request Mar 11, 2026
Co-authored-by: 越鸿 <nishenghao.nsh@oceanbase.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants