How an API error became a successful workflow result
A truncated API key gave me an authentication error while I was building a small Temporal-backed agent. The surprising part was the Workflow status: one of the examples I was using could finish successfully and return th
A truncated API key gave me an authentication error while I was building a small Temporal-backed agent. The surprising part was the Workflow status: one of the examples I was using could finish successfully and return the authentication error as its answer.
The API had rejected the request. The agent still produced a terminal result message. The example returned the message's text, so Temporal received a normal return value.
This was an error-handling omission in the examples. Temporal had no way to infer that a string returned by application code represented failure.
A result message needs interpretation
The affected samples used the Claude Agent SDK through Temporal's integration. Their response loops waited for a ResultMessage, then returned message.result.
That field can contain text explaining why a turn failed. Receiving the message only tells the application that the turn ended; the application also needs to check is_error.
In the hooks example, a rejected key could therefore produce a successful Workflow whose result was an authentication-error string. The structured-output example failed differently: it looked for structured data, found none, and raised an error about missing output. That obscured the authentication failure which had caused the missing data.
Those are different symptoms of the same boundary: the code interpreted the answer before checking whether the turn had failed.
Reproducing it without a live model
The samples already had a scripted, local API for offline testing. I extended that fixture so it could return an HTTP error as well as a model response. For this regression, every request received a 401 authentication error with a fixed test message.
The fixture used test credentials and a local endpoint. It did not need a valid model API key or an external inference request.
I ran that rejected-key case through the example Workflows and the chat example's message Update. Before the change, nine of the ten cases treated error text as a successful result. The structured-output case raised the misleading missing-output error instead.
The regression tests check more than whether something raised:
- The scripted API must have received a request, so an unrelated setup failure cannot satisfy the test.
- The failure must reach the caller as a Temporal
ApplicationErrorwith a non-empty reason. - For chat, the message Update must fail rather than deliver the error string as a normal reply.
The tests describe behavior at that pinned revision. They are not a claim about every SDK version or every kind of failed turn.
Check the failure flag before using the answer
The response handler now checks is_error first. On failure, it takes the reason from result, falling back to the errors list, and raises an ApplicationError. Only a successful result proceeds to normal answer handling.
The same ordering matters for structured output: check for a failed turn before diagnosing a missing structured value. Otherwise the useful cause is replaced by a secondary error.
In the chat sample, a failed message Update also leaves the reference to the last good reply unchanged. An error should not become the conversation's new successful answer.
This change makes a failed turn visible to the Workflow or Update caller. It does not decide whether every failure should be retried. Authentication errors, rate limits, connection loss and temporary provider outages need different policies. Raising an error and choosing a retry policy are separate decisions.
A retry detail in the test harness
The CLI could retry the rejected request with backoff, making this deliberately failing test slow. The offline harness sets CLAUDE_CODE_MAX_RETRIES=0: its scripted response is immediate and final, so repeating it adds no information.
That setting belongs to this test setup. It is not a recommendation to disable retries for production provider calls.
On 12 November, after the change, the ten new cases passed locally; the Claude example suite had 37 passing tests. The proposed samples fix, PR #411, has received approval but remains open as of 16 November. Local results do not establish that the repository's full CI is green.
A completed SDK turn, a successful model request and a successful business operation are different outcomes. Here, checking the result message's failure flag was enough to stop one from being mistaken for the others.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.