eca icon indicating copy to clipboard operation
eca copied to clipboard

Streaming: tool calls are silently dropped when a provider reports `stop_reason: "end_turn"`

Open sudo-human opened this issue 3 months ago • 2 comments

Version: 0.154.2 Provider config: custom provider, "api": "anthropic", pointed at an Anthropic-compatible corporate gateway

Summary

When a streamed response contains a complete tool_use content block but the terminal message_delta reports stop_reason: "end_turn" instead of "tool_use", ECA finishes the turn and discards the pending tool call. No tool runs, no error surfaces, and the chat simply stops mid-task.

The tool call is fully parsed and already sitting in @content-block* when this happens — ECA just never looks at it, because the dispatch is keyed solely on stop_reason.

Impact

Against a gateway with this quirk, every non-Claude model becomes unusable for anything agentic. Plain chat works fine (a chat turn genuinely is end_turn, so the wrong label is harmless), which makes the failure look intermittent and model-specific rather than systematic. In practice the assistant appears to "stop working when reading or writing files".

Because nothing is logged as an error, this is quite hard to diagnose from the UI — it reads as the model giving up.

Root cause

src/eca/llm_providers/anthropic.clj:523

(case (-> data :delta :stop_reason)
  "tool_use"  (let [tool-calls (keep
                                (fn [content-block]
                                  (when (= "tool_use" (:type content-block))
                                    {:id (:id content-block) ...}))
                                (vals @content-block*))]
                ;; execute tools and recurse via base-request!
                ...)
  "end_turn"  (if @has-content?*
                (do
                  (reset! content-block* {})
                  (on-message-received {:type :finish
                                        :finish-reason (-> data :delta :stop_reason)}))
                (throw (ex-info "Stream ended with empty response"
                                {:error/type :premature-stop})))
  ...)

The "end_turn" branch resets content-block* and finishes without checking whether any of those blocks were tool_use. Note that has-content?* is already set to true by the tool_use case in content_block_start (line 454-459), so the guard passes and we take the :finish path.

Reproduction

Any provider that emits this shape. Minimal SSE sequence:

event: content_block_start
data: {"index":0,"content_block":{"type":"tool_use","id":"read_file_0",
       "name":"read_file","input":{}}}

event: content_block_delta
data: {"index":0,"delta":{"type":"input_json_delta",
       "partial_json":"{\"path\": \"/tmp/a.txt\"}"}}

event: content_block_stop
data: {"index":0}

event: message_delta
data: {"delta":{"stop_reason":"end_turn"}}      ← should be "tool_use"

event: message_stop

Expected: ECA executes read_file and continues the loop. Actual: ECA finishes the turn; the tool never runs.

In my case the upstream gateway is at fault — it maps OpenAI's finish_reason: "tool_calls" correctly in non-streaming responses but drops it when synthesizing the streaming message_delta. I have reported that separately. But since ECA has the tool call in hand, it seems worth not depending on the provider getting this field right.

Suggested fix

Normalize the stop reason before dispatching: if any accumulated content block is a tool_use, treat the turn as "tool_use" regardless of what the provider reported.

(let [reported (-> data :delta :stop_reason)
      pending-tool-use? (some #(= "tool_use" (:type %)) (vals @content-block*))
      ;; Some Anthropic-compatible gateways mislabel a tool-calling turn as
      ;; "end_turn". Trust the content blocks over the reported reason:
      ;; finishing here would silently drop the tool call.
      stop-reason (if (and pending-tool-use?
                           (contains? #{"end_turn" "stop"} reported))
                    "tool_use"
                    reported)]
  (case stop-reason
    "tool_use" ...
    "end_turn" ...))

This is a no-op for compliant providers: a correct "end_turn" turn has no tool_use blocks pending, so the condition never fires.

Two notes on scope:

  • "max_tokens" should probably not be coerced — a tool call truncated mid-arguments is genuinely incomplete, and the existing overflow handling is the right response.
  • If you would rather not paper over provider bugs silently, a logger/warn when the coercion fires would make the underlying problem visible while keeping the session working. I would find that preferable to failing closed, but that is your call.

Happy to send a PR if the approach looks right.

Environment

  • ECA 0.154.2
  • eca-emacs (latest from main), Emacs 32.0.50, macOS 15 (Darwin 25.5.0)
  • Custom provider, api: "anthropic", corporate gateway fronting Anthropic, OpenAI and Fireworks models behind one Messages-compatible endpoint

Verified matrix on that gateway — models routed to Anthropic upstream are correct, everything translated is not:

Model family streaming stop_reason with tool call
vertex/claude-* (native passthrough) tool_use ✅
azure/gpt-* (translated) end_turn ❌
fireworks/* (translated) end_turn ❌

For contrast, Claude Code / claude-agent-acp against the same gateway completes these turns successfully — it continues the loop when tool_use blocks are present rather than keying on stop_reason alone.

sudo-human avatar Aug 19 '26 14:08 sudo-human

Thanks, I did a improvement, LMK if works!

ericdallo avatar Aug 20 '26 12:08 ericdallo

@ericdallo Thanks, works like a charm!

sudo-human avatar Aug 21 '26 06:08 sudo-human