Skip to content

[Bug/Robustness] 400 Error: InternalError.Algo.InvalidParameter due to unclosed tool_calls after interruption #22808

Closed as not planned

Description

@zhg4554

Description

[Bug/Robustness] 400 Error: InternalError.Algo.InvalidParameter due to unclosed tool_calls after interruption

Description

When using OpenCode with Alibaba DashScope (阿里云百炼) or similar providers, the session becomes permanently corrupted if an agent fails to provide a tool response following an assistant message that contains tool_calls.

This typically occurs in multi-agent workflows when:

  1. An agent crashes or timed out after generating a tool call.
  2. The system/process is interrupted or rebooted before the tool execution completes.

Upon restarting the session, the API returns a 400 Bad Request:
"An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'."

Root Cause

Alibaba DashScope's inference engine (and OpenAI's strict mode) enforces a strict transactional sequence for tool calls. If the conversation history sent to the API ends with a "dangling" tool_calls without corresponding tool results, the API rejects the entire payload.

Proposed Solution: The "Tombstone" Pattern (Preferred)

I recommend implementing a Self-Healing/Auto-Completion mechanism within the framework's message persistence or pre-request layer.

Logic:
Before dispatching the messages array to the LLM API, the framework should validate the integrity of the tool-call chain. If a dangling tool_call_id is detected, OpenCode should automatically inject a "Tombstone" (placeholder) message to close the loop.

Example Injected Payload:

{
  "role": "tool",
  "tool_call_id": "the_interrupted_call_id",
  "content": "{\"error\": \"System interruption: The agent/tool failed to return a result in the previous session.\"}"
}

Plugins

No response

OpenCode version

1.4.6

Steps to reproduce

interrupt opencode when it is calling tool. (actually, I do not interrupt it,maybe the internal tool interrupt)

Screenshot and/or share link

Image

"<400> InternalError.Algo.InvalidParameter: An assistant message with "tool_calls" must be followed by tool messages responding to each "tool_call_id"."

Operating System

windows10

Terminal

git bash

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Apr 16, 2026
  2. github-actions commented on Apr 16, 2026

    @github-actions
    Contributor

    This issue doesn't fully meet our contributing guidelines.

    What needs to be fixed:

    • Steps to reproduce are missing — bug reports should include some context on how to trigger the issue
    • System information (OpenCode version, Operating System, Terminal) are all left as _No response_ — please provide your environment details so maintainers can investigate

    Please edit this issue to address the above within 2 hours, or it will be automatically closed.


    This issue might be a duplicate of existing issues. Please check:

    If you believe this was flagged incorrectly, please let a maintainer know.

  3. github-actions commented on Apr 16, 2026

    @github-actions
    Contributor

    Thanks for updating the issue description — it now includes a clear explanation of the bug and root cause.

    However, the following required fields are still left as _No response_ (placeholder text):

    • Steps to reproduce — please describe how to trigger this issue
    • OpenCode version — which version are you running?
    • Operating System — e.g. macOS 14, Ubuntu 22.04
    • Terminal — e.g. iTerm2, Warp, default terminal

    Please edit the issue to fill in these fields. System information is important so maintainers can investigate and reproduce the problem.

  4. github-actions commented on Apr 16, 2026

    @github-actions
    Contributor

    Thank you for updating the issue! It now contains all the required information and looks good. We appreciate you including the error details, screenshot, and system information.

  5. removed
    bugSomething isn't working
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  6. github-actions commented on Jul 18, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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