Commit 246c4ae
authored
fix(providers): detect truncated Anthropic and OpenAI Responses streams (#33)
The Anthropic Messages streaming protocol guarantees message_stop as the
final SSE event of every successful stream. Today the adapter treats any
clean EOF (Stream.Err() == nil or io.EOF) as a successful Finish, even
when the upstream body was cut off mid-response. This silently truncates
the assistant's reply and commits the partial text as if it were the
model's complete answer.
Track whether message_stop was observed during the SSE loop. On clean
EOF without it, yield StreamPartTypeError wrapping io.EOF so the failure
surfaces as a retryable transport error rather than a phantom success.
Existing transport errors continue to flow through the unchanged else
branch; the event: error path keeps yielding via Stream.Err().
Tests cover happy path, EOF before message_stop, empty stream, and
malformed stream (existing error path preserved).
Also picks up a one-line gofmt fix in TestComputerUseToolJSON; the test
file was not gofmt-clean at HEAD without it.
* fix(providers/openai): require terminal Responses event before Finish
The OpenAI Responses API emits terminal lifecycle events when a streamed
response reaches its final state. The adapter currently yields Finish on
any clean EOF, even if the stream ended before response.completed or
response.incomplete. That has the same silent-truncation shape as the
Anthropic message_stop bug in this PR.
Track response.completed and response.incomplete before yielding Finish
from both Stream and StreamObject. If the transport closes cleanly first,
yield a StreamPartTypeError/ObjectStreamPartTypeError wrapping io.EOF so
callers can retry instead of committing partial output. Also surface
response.failed as an error event instead of falling through to Finish.
Tests cover completed and incomplete terminal events, EOF before terminal
event, empty streams, response.failed, malformed streams, and JSON-mode
StreamObject truncation.
Also fixes a pre-existing OpenAI test compile issue where one
toResponsesPrompt call still expected two return values.1 parent d0e6ce2 commit 246c4ae
4 files changed
Lines changed: 395 additions & 4 deletions
File tree
- providers
- anthropic
- openai
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1244 | 1244 | | |
1245 | 1245 | | |
1246 | 1246 | | |
| 1247 | + | |
1247 | 1248 | | |
1248 | 1249 | | |
1249 | 1250 | | |
| |||
1448 | 1449 | | |
1449 | 1450 | | |
1450 | 1451 | | |
| 1452 | + | |
1451 | 1453 | | |
1452 | 1454 | | |
1453 | 1455 | | |
1454 | 1456 | | |
1455 | 1457 | | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
1456 | 1468 | | |
1457 | 1469 | | |
1458 | 1470 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
504 | 505 | | |
505 | 506 | | |
506 | 507 | | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
507 | 599 | | |
508 | 600 | | |
509 | 601 | | |
| |||
563 | 655 | | |
564 | 656 | | |
565 | 657 | | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
566 | 681 | | |
567 | 682 | | |
568 | 683 | | |
| |||
1574 | 1689 | | |
1575 | 1690 | | |
1576 | 1691 | | |
1577 | | - | |
| 1692 | + | |
| 1693 | + | |
1578 | 1694 | | |
1579 | 1695 | | |
1580 | 1696 | | |
| |||
0 commit comments