Anthropic 400: thinking field is null when thinking block had no text deltas
When using anthropic models via anthropic, I started getting these errors today after first "thinking". I switched to using sonnet-4-6 via github-copilot and I just asked it to investigate, but it also submitted this on my behalf before I had time to test further. I have now tested the fix locally and it seems to work. I'll keep using it and report if the problem repeats.
Bug
When a conversation contains a stored reason message from an Anthropic thinking block that accumulated zero thinking_delta events (i.e. the model returned an empty thinking block), ECA sends null for the thinking field on the next request. Anthropic's API rejects this with:
Anthropic response status: 400 body: {"type":"error","error":{"type":"invalid_request_error","message":"messages.3.content.0.thinking.thinking: Input should be a valid string"},"request_id":"req_011Cbe6NenQJS91qdUN3DS3L"}
Root cause
In src/eca/llm_providers/anthropic.clj, when serializing a stored "reason" message back into an outbound request:
{:type "thinking"
:signature (:external-id content)
:thinking (:text content)} ; <-- nil when no thinking_delta was received
(:text reasoning) starts as nil in the reasonings* atom and is only set via:
:thinking (do (swap! reasonings* update-in [id :text] str text) ...)
If no :thinking delta arrives before :finished (empty thinking block), (:text reasoning) remains nil.
Fix
Change line ~242 from:
:thinking (:text content)
to:
:thinking (or (:text content) "")
I cloned the repo, built locally, copied patched eca server, restarted server, resumed a chat that previously today everytime ended in the error, and now it works. Also new chats go beoynd two "thinking"s.
Environment
- ECA version: 0.136.4
- Provider: anthropic (direct)
- Model producing the error: claude-* with extended thinking enabled
I have now tested that the fix works and all unit tests pass. I keep using the patched eca server to check that the error doesn't come back