Repository navigation
Commit 3725fa3
fix(openai): preserve async parsing for streaming responses (#607)
resolves
https://linear.app/braintrustdata/issue/SDK-86/async-streaming-response-parsing-not-preserved
## Summary
- await asynchronous raw-response parsing before wrapping OpenAI
Responses and Chat Completions streams
- preserve the awaitable `parse()` contract exposed by
`with_streaming_response`
- extend existing cassette-backed streaming tests to await and consume
the traced streams without mocks or new cassette churn
## Root cause
`openai.AsyncAPIResponse.parse()` is asynchronous, but the tracing proxy
replaced it with a synchronous method that returned the traced stream
directly. The wrappers also attempted to instrument the un-awaited
parser coroutine rather than the parsed iterator.
This broke callers such as OpenAI Agents that enter
`with_streaming_response` and then call `await api_response.parse()`.
Chat Completions had the same issue on its async raw streaming path.
The wrappers now detect and await asynchronous parsing before
constructing the traced iterator, remember the original parse contract,
and expose an awaitable `parse()` on the raw-response proxy. Synchronous
raw-response behavior remains unchanged.
## Validation
- reproduced the Chat Completions failure with existing VCR coverage:
`parse()` was not awaitable and the underlying parser coroutine was
never awaited
- full `test_openai(latest)` session with cassette playback: 70 tests
passed, plus attachment and OpenRouter gateway suites
- focused cassette playback for OpenAI 1.92.0, 1.77.0, and 1.71.0
- pre-commit hooks: Ruff format/check, cassette consistency, pytest-pin
synchronization, codespell, and whitespace checks
---------
Co-authored-by: Abhijeet Prasad <abhijeet@braintrustdata.com>1 parent de233f8 commit 3725fa3
2 files changed
Lines changed: 55 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1066 | 1066 | | |
1067 | 1067 | | |
1068 | 1068 | | |
1069 | | - | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
1074 | | - | |
1075 | | - | |
1076 | | - | |
1077 | | - | |
1078 | | - | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
1079 | 1086 | | |
1080 | 1087 | | |
1081 | 1088 | | |
| |||
1978 | 1985 | | |
1979 | 1986 | | |
1980 | 1987 | | |
1981 | | - | |
| 1988 | + | |
1982 | 1989 | | |
1983 | 1990 | | |
1984 | 1991 | | |
| |||
1996 | 2003 | | |
1997 | 2004 | | |
1998 | 2005 | | |
1999 | | - | |
| 2006 | + | |
2000 | 2007 | | |
2001 | 2008 | | |
2002 | | - | |
| 2009 | + | |
2003 | 2010 | | |
2004 | 2011 | | |
2005 | 2012 | | |
2006 | | - | |
2007 | | - | |
2008 | | - | |
2009 | | - | |
2010 | | - | |
2011 | | - | |
2012 | | - | |
2013 | | - | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
2014 | 2023 | | |
2015 | 2024 | | |
2016 | 2025 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
499 | 499 | | |
500 | 500 | | |
501 | 501 | | |
| 502 | + | |
502 | 503 | | |
503 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
504 | 508 | | |
505 | 509 | | |
506 | 510 | | |
| |||
529 | 533 | | |
530 | 534 | | |
531 | 535 | | |
532 | | - | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
533 | 541 | | |
534 | 542 | | |
535 | 543 | | |
| |||
844 | 852 | | |
845 | 853 | | |
846 | 854 | | |
847 | | - | |
| 855 | + | |
848 | 856 | | |
| 857 | + | |
849 | 858 | | |
850 | 859 | | |
851 | 860 | | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
852 | 866 | | |
853 | 867 | | |
854 | 868 | | |
| |||
1066 | 1080 | | |
1067 | 1081 | | |
1068 | 1082 | | |
| 1083 | + | |
1069 | 1084 | | |
1070 | 1085 | | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
1071 | 1089 | | |
1072 | 1090 | | |
1073 | 1091 | | |
| |||
1097 | 1115 | | |
1098 | 1116 | | |
1099 | 1117 | | |
1100 | | - | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
1101 | 1123 | | |
1102 | 1124 | | |
1103 | 1125 | | |
| |||
0 commit comments