Parse Anthropic SSE events into (event_type, data) tuples.
(raw_events: list[bytes])
| 559 | |
| 560 | |
| 561 | def _parse_anthropic_events(raw_events: list[bytes]) -> list[dict]: |
| 562 | """Parse Anthropic SSE events into (event_type, data) tuples.""" |
| 563 | results: list[dict] = [] |
| 564 | for raw in raw_events: |
| 565 | text = raw.decode() |
| 566 | lines = text.strip().split("\n") |
| 567 | event_type = "" |
| 568 | data_str = "" |
| 569 | for line in lines: |
| 570 | if line.startswith("event: "): |
| 571 | event_type = line[7:] |
| 572 | elif line.startswith("data: "): |
| 573 | data_str = line[6:] |
| 574 | if data_str: |
| 575 | parsed = json.loads(data_str) |
| 576 | parsed["_event"] = event_type |
| 577 | results.append(parsed) |
| 578 | return results |
| 579 | |
| 580 | |
| 581 | def _parse_openai_events(raw_events: list[bytes]) -> tuple[list[dict], bool]: |
no test coverage detected