fix(anthropic): require message_stop before finishing stream#245
Draft
ethanndickson wants to merge 1 commit into
Draft
fix(anthropic): require message_stop before finishing stream#245ethanndickson wants to merge 1 commit into
ethanndickson wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
#232 added
fantasy.NewIncompleteStreamError()and gated the Anthropic post-loop onacc.StopReason == "". That catches the user-facing-content cases: a stream that closes anywhere from the firstcontent_block_deltaup to (but not including) themessage_deltaevent leavesacc.StopReasonempty and is correctly reported as a retryable incomplete stream.What that check still admits is the narrow window between
message_deltaandmessage_stop.message_deltacarries thestop_reasonand setsacc.StopReasonnon-empty, so the existing guard passes; but the protocol-requiredmessage_stopterminator never arrives. The stream then emitsStreamPartTypeFinishwith a real-looking finish reason, hiding the fact that the transport closed unexpectedly in a window the Anthropic SSE protocol documents as impossible on success.Fix
Require both terminal signals before yielding
Finish:sawMessageStopbool is flipped totrueinside the chunk-type switch when amessage_stopevent arrives.sawMessageStopis false oracc.StopReasonis empty, the stream yieldsfantasy.NewIncompleteStreamError()(retryable*fantasy.ProviderErrorwithCause: io.ErrUnexpectedEOF) instead ofFinish.ctx.Err()takes precedence so caller-initiated cancellations stay distinguishable from synthetic transport errors — a small attribution improvement on the existing path as well.This matches the pattern Anthropic ships in their own newest SDK:
MessageAccumulatorinanthropic-sdk-java(PR anthropics/anthropic-sdk-java#178) raisesIllegalStateException("'message_stop' event not yet received.")onMessageAccumulator.message()and has dedicated unit tests for bothmessageNotStartedandmessageNotStopped.Tests
A new table-driven test
TestStream_RequiresMessageStopBeforeFinishcovers:Finishmessage_deltawithstop_reasonpresent butmessage_stopmissing → retryable incomplete-stream error (the new window this PR closes)message_stoppresent butstop_reasonmissing → retryableerrorSSE event → surfaced verbatim and not retryableThe pre-existing
TestStream_TruncatedWithoutStopReason(from #232) covers the simpler case where neither terminal event arrives and is unchanged.Follow-ups
Similar terminal-event gaps exist in a few of the other providers in this repo; those will land in separate PRs so each can be reviewed in isolation.