Skip to content

Commit dc2a2b4

Browse files
fix(ai): record mid-stream errors on streamed OpenAI, Anthropic and Gemini events (#1094)
* fix(ai): record mid-stream errors on streamed OpenAI, Anthropic and Gemini events capture_streaming_event hard-coded $ai_http_status=200, and the stream generators only had a finally block. When the provider stream raised after the first chunk (overloaded, connection reset), the event was sent as a success with no $ai_is_error/$ai_error, unlike the non-streaming path. Catch the exception in each sync and async generator, pass it through StreamingEventData, and re-raise unchanged. The event now carries $ai_is_error, $ai_error and the error's status code (0 when it has none). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(ai): pass the stream error to StreamingEventData directly and update the API snapshot mypy 1.16 rejected splatting {"error": error} into StreamingEventData(...) (typeddict-item). The field accepts None and capture_streaming_event reads it with .get(), so pass error=error. Regenerate the public API snapshot for the new StreamingEventData.error field. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent e9710ec commit dc2a2b4

13 files changed

Lines changed: 339 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
pypi/posthog: patch
3+
---
4+
5+
Streamed OpenAI, Anthropic and Gemini generations that raise mid-stream are now captured with `$ai_is_error` and `$ai_error` (and the error's status code as `$ai_http_status`) instead of being recorded as a successful 200. The error is still re-raised to the caller, matching what non-streaming calls already did.

‎posthog/ai/anthropic/anthropic.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ def _track_streaming_response(
188188
accumulator = _AnthropicStreamAccumulator()
189189

190190
def generator():
191+
stream_error: Optional[BaseException] = None
191192
try:
192193
for event in response:
193194
accumulator.consume(event)
194195
yield event
196+
except Exception as exc:
197+
stream_error = exc
198+
raise
195199
finally:
196200
end_time = time.time()
197201
latency = end_time - start_time
@@ -208,6 +212,7 @@ def generator():
208212
accumulator.content_blocks,
209213
accumulator.accumulated_content,
210214
stop_reason=accumulator.stop_reason,
215+
error=stream_error,
211216
)
212217

213218
return _StreamWrapper(generator(), stream=response)
@@ -225,6 +230,7 @@ def _capture_streaming_event(
225230
content_blocks: List[StreamingContentBlock],
226231
accumulated_content: str,
227232
stop_reason: Optional[str] = None,
233+
error: Optional[BaseException] = None,
228234
):
229235
from posthog.ai.types import StreamingEventData
230236
from posthog.ai.anthropic.anthropic_converter import (
@@ -252,6 +258,7 @@ def _capture_streaming_event(
252258
privacy_mode=posthog_privacy_mode,
253259
groups=posthog_groups,
254260
stop_reason=stop_reason,
261+
error=error,
255262
)
256263

257264
# Use the common capture function

‎posthog/ai/anthropic/anthropic_async.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ def _track_streaming_response(
188188
accumulator = _AnthropicStreamAccumulator()
189189

190190
async def generator():
191+
stream_error: Optional[BaseException] = None
191192
try:
192193
async for event in response:
193194
accumulator.consume(event)
194195
yield event
196+
except Exception as exc:
197+
stream_error = exc
198+
raise
195199
finally:
196200
end_time = time.time()
197201
latency = end_time - start_time
@@ -208,6 +212,7 @@ async def generator():
208212
accumulator.content_blocks,
209213
accumulator.accumulated_content,
210214
stop_reason=accumulator.stop_reason,
215+
error=stream_error,
211216
)
212217

213218
return AsyncStreamWrapper(generator(), stream=response)
@@ -225,6 +230,7 @@ async def _capture_streaming_event(
225230
content_blocks: List[StreamingContentBlock],
226231
accumulated_content: str,
227232
stop_reason: Optional[str] = None,
233+
error: Optional[BaseException] = None,
228234
):
229235
from posthog.ai.types import StreamingEventData
230236
from posthog.ai.anthropic.anthropic_converter import (
@@ -252,6 +258,7 @@ async def _capture_streaming_event(
252258
privacy_mode=posthog_privacy_mode,
253259
groups=posthog_groups,
254260
stop_reason=stop_reason,
261+
error=error,
255262
)
256263

257264
# Use the common capture function

‎posthog/ai/gemini/_shared.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def _capture_streaming_event(
203203
latency: float,
204204
output: Any,
205205
stop_reason: Optional[str] = None,
206+
error: Optional[BaseException] = None,
206207
) -> None:
207208
formatted_input = merge_system_prompt(
208209
{"contents": contents, **kwargs}, "gemini"
@@ -222,6 +223,7 @@ def _capture_streaming_event(
222223
privacy_mode=privacy_mode,
223224
groups=groups,
224225
stop_reason=stop_reason,
226+
error=error,
225227
)
226228
capture_streaming_event(self._ph_client, event_data)
227229

‎posthog/ai/gemini/gemini.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def generator():
266266
nonlocal usage_stats
267267
nonlocal accumulated_content
268268
nonlocal stop_reason
269+
stream_error: Optional[BaseException] = None
269270
try:
270271
for chunk in response:
271272
# Extract usage stats from chunk
@@ -288,6 +289,9 @@ def generator():
288289

289290
yield chunk
290291

292+
except Exception as exc:
293+
stream_error = exc
294+
raise
291295
finally:
292296
end_time = time.time()
293297
latency = end_time - start_time
@@ -305,6 +309,7 @@ def generator():
305309
latency,
306310
accumulated_content,
307311
stop_reason=stop_reason,
312+
error=stream_error,
308313
)
309314

310315
return generator()

‎posthog/ai/gemini/gemini_async.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ async def async_generator():
253253
nonlocal accumulated_content
254254
nonlocal stop_reason
255255

256+
stream_error: Optional[BaseException] = None
256257
try:
257258
async for chunk in response:
258259
# Extract usage stats from chunk
@@ -275,6 +276,9 @@ async def async_generator():
275276

276277
yield chunk
277278

279+
except Exception as exc:
280+
stream_error = exc
281+
raise
278282
finally:
279283
end_time = time.time()
280284
latency = end_time - start_time
@@ -292,6 +296,7 @@ async def async_generator():
292296
latency,
293297
accumulated_content,
294298
stop_reason=stop_reason,
299+
error=stream_error,
295300
)
296301

297302
return AsyncStreamWrapper(async_generator(), stream=response)

‎posthog/ai/openai/_streaming.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def _build_streaming_event_data(
102102
model_from_response: Optional[str],
103103
stop_reason: Optional[str],
104104
service_tier: Optional[str] = None,
105+
error: Optional[BaseException] = None,
105106
) -> StreamingEventData:
106107
"""Build the fields shared by both OpenAI streaming endpoint events."""
107108

@@ -121,4 +122,5 @@ def _build_streaming_event_data(
121122
groups=groups,
122123
stop_reason=stop_reason,
123124
service_tier=service_tier,
125+
error=error,
124126
)

‎posthog/ai/openai/openai.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ def _create_streaming(
170170
response = self._original.create(**kwargs)
171171

172172
def generator():
173+
stream_error: Optional[BaseException] = None
173174
try:
174175
for chunk in response:
175176
state.process_chunk(chunk)
176177
yield chunk
178+
except Exception as exc:
179+
stream_error = exc
180+
raise
177181
finally:
178182
self._capture_streaming_event(
179183
posthog_distinct_id,
@@ -184,6 +188,7 @@ def generator():
184188
kwargs,
185189
state,
186190
time.time() - start_time,
191+
error=stream_error,
187192
)
188193

189194
return _StreamWrapper(generator(), stream=response)
@@ -198,6 +203,7 @@ def _capture_streaming_event(
198203
kwargs: Dict[str, Any],
199204
state: _ResponsesStreamState,
200205
latency: float,
206+
error: Optional[BaseException] = None,
201207
):
202208
from posthog.ai.utils import capture_streaming_event
203209

@@ -216,6 +222,7 @@ def _capture_streaming_event(
216222
model_from_response=state.model,
217223
stop_reason=state.stop_reason,
218224
service_tier=state.service_tier,
225+
error=error,
219226
)
220227
capture_streaming_event(self._client._ph_client, event_data)
221228

@@ -388,10 +395,14 @@ def _create_streaming(
388395
response = self._original.create(**kwargs)
389396

390397
def generator():
398+
stream_error: Optional[BaseException] = None
391399
try:
392400
for chunk in response:
393401
state.process_chunk(chunk)
394402
yield chunk
403+
except Exception as exc:
404+
stream_error = exc
405+
raise
395406
finally:
396407
self._capture_streaming_event(
397408
posthog_distinct_id,
@@ -402,6 +413,7 @@ def generator():
402413
kwargs,
403414
state,
404415
time.time() - start_time,
416+
error=stream_error,
405417
)
406418

407419
return _StreamWrapper(generator(), stream=response)
@@ -416,6 +428,7 @@ def _capture_streaming_event(
416428
kwargs: Dict[str, Any],
417429
state: _ChatCompletionsStreamState,
418430
latency: float,
431+
error: Optional[BaseException] = None,
419432
):
420433
from posthog.ai.utils import capture_streaming_event
421434

@@ -436,6 +449,7 @@ def _capture_streaming_event(
436449
model_from_response=state.model,
437450
stop_reason=state.stop_reason,
438451
service_tier=state.service_tier,
452+
error=error,
439453
)
440454
capture_streaming_event(self._client._ph_client, event_data)
441455

‎posthog/ai/openai/openai_async.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ async def _create_streaming(
171171
response = await self._original.create(**kwargs)
172172

173173
async def async_generator():
174+
stream_error: Optional[BaseException] = None
174175
try:
175176
async for chunk in response:
176177
state.process_chunk(chunk)
177178
yield chunk
179+
except Exception as exc:
180+
stream_error = exc
181+
raise
178182
finally:
179183
await self._capture_streaming_event(
180184
posthog_distinct_id,
@@ -185,6 +189,7 @@ async def async_generator():
185189
kwargs,
186190
state,
187191
time.time() - start_time,
192+
error=stream_error,
188193
)
189194

190195
return AsyncStreamWrapper(async_generator(), stream=response)
@@ -199,6 +204,7 @@ async def _capture_streaming_event(
199204
kwargs: Dict[str, Any],
200205
state: _ResponsesStreamState,
201206
latency: float,
207+
error: Optional[BaseException] = None,
202208
):
203209
from posthog.ai.utils import capture_streaming_event
204210

@@ -217,6 +223,7 @@ async def _capture_streaming_event(
217223
model_from_response=state.model,
218224
stop_reason=state.stop_reason,
219225
service_tier=state.service_tier,
226+
error=error,
220227
)
221228
capture_streaming_event(self._client._ph_client, event_data)
222229

@@ -392,10 +399,14 @@ async def _create_streaming(
392399
response = await self._original.create(**kwargs)
393400

394401
async def async_generator():
402+
stream_error: Optional[BaseException] = None
395403
try:
396404
async for chunk in response:
397405
state.process_chunk(chunk)
398406
yield chunk
407+
except Exception as exc:
408+
stream_error = exc
409+
raise
399410
finally:
400411
await self._capture_streaming_event(
401412
posthog_distinct_id,
@@ -406,6 +417,7 @@ async def async_generator():
406417
kwargs,
407418
state,
408419
time.time() - start_time,
420+
error=stream_error,
409421
)
410422

411423
return AsyncStreamWrapper(async_generator(), stream=response)
@@ -420,6 +432,7 @@ async def _capture_streaming_event(
420432
kwargs: Dict[str, Any],
421433
state: _ChatCompletionsStreamState,
422434
latency: float,
435+
error: Optional[BaseException] = None,
423436
):
424437
from posthog.ai.utils import capture_streaming_event
425438

@@ -440,6 +453,7 @@ async def _capture_streaming_event(
440453
model_from_response=state.model,
441454
stop_reason=state.stop_reason,
442455
service_tier=state.service_tier,
456+
error=error,
443457
)
444458
capture_streaming_event(self._client._ph_client, event_data)
445459

‎posthog/ai/types.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ class StreamingEventData(TypedDict):
143143
groups: Optional[Dict[str, Any]]
144144
stop_reason: Optional[str]
145145
service_tier: NotRequired[Optional[str]]
146+
error: NotRequired[Optional[BaseException]] # raised mid-stream, if any

0 commit comments

Comments
 (0)