:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import json
from collections.abc import AsyncGenerator
from typing import Any
import structlog
logger = structlog.get_logger(__name__)
def _serialize_chunk(obj: Any) -> Any:
if isinstance(obj, bytes):
return obj.decode("utf-8")
if isinstance(obj, dict):
return {k: _serialize_chunk(v) for k, v in obj.items()}
if isinstance(obj, list):
return [_serialize_chunk(item) for item in obj]
return obj
async def format_sse_stream(llm_stream: AsyncGenerator[Any]) -> AsyncGenerator[bytes]:
is_raw_passthrough = None
try:
async for chunk in llm_stream:
if isinstance(chunk, bytes):
if is_raw_passthrough is None:
is_raw_passthrough = True
yield chunk
continue
if is_raw_passthrough is None:
is_raw_passthrough = False
chunk_dict = chunk.model_dump() if hasattr(chunk, "model_dump") else chunk
serializable = _serialize_chunk(chunk_dict)
yield f"data: {json.dumps(serializable)}\n\n".encode()
if not is_raw_passthrough:
yield b"data: [DONE]\n\n"
except Exception as e:
logger.exception("Error in LLM stream")
error_data = {
"error": {
"message": getattr(e, "message", str(e)),
"type": getattr(e, "type", "internal_error"),
"code": getattr(e, "code", None),
}
}
yield f"data: {json.dumps(error_data)}\n\n".encode()
raise