OpenHands / OpenHands/software-agent-sdk

run-examples: 28_ask_agent_example.py fails with Anthropic 400 (orphaned tool_use without tool_result)

Open
#4,653 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug llm priority:medium sdk
Dominant language
Python
Stars
1.1k
Forks
539
Avg merge
1d 19h
Merged PRs (30d)
137

Description

The run-examples workflow is failing on examples/01_standalone_sdk/28_ask_agent_example.py.

Symptom

The example fails with an Anthropic 400 invalid_request_error surfaced through LiteLLM/OpenAI as openai.BadRequestError:

messages.6: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_01DqrY27EqjnoefvSxz3uwz6, ... Each `tool_use` block must have a corresponding `tool_result` block in the next message.

...mapped to openhands.sdk.llm.exceptions.types.LLMMalformedConversationHistoryError, and reported by the harness as ConversationRunError.

Root cause

Conversation.ask_agent() builds its LLM context from the cached, incrementally-maintained self.state.view. That view skips enforce_properties on linear appends, so it can transiently contain an ActionEvent whose matching ObservationEvent has not landed yet — i.e. a tool call that the main agent is still executing on another thread (this example deliberately calls ask_agent() mid-run, on a background thread while the "Explore the current directory" task is in flight).

When that orphaned tool_use reaches Anthropic with no following tool_result, it is rejected with a 400. A fresh View.from_events(active_branch) re-runs property enforcement and drops the orphaned action before serialization.

Fix

Already available (open, unmerged): #4630 — fix(sdk): drop in-flight tool calls from ask_agent context, which builds the ask-agent context from View.from_events(self._state.active_branch()) instead of the cached view.

Evidence


This issue was created by an AI agent (OpenHands) on behalf of a user.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with examples/01_standalone_sdk/28_ask_agent_example.py and inspect Conversation.ask_agent(), then compare the context construction with the approach described in pull request #4630. Run the example or the run-examples workflow and confirm that the in-flight tool call no longer produces an Anthropic 400 or ConversationRunError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.