{"record":{"id":"316f15a43dd88b26","repo":"rohitg00/ai-engineering-from-scratch","slug":"session-emitted-an-error-event","errorCode":null,"errorMessage":"session emitted an error event","messagePattern":"session emitted an error event","errorType":"exception","errorClass":"EventStreamError","httpStatus":null,"severity":"error","filePath":"certifications/claude/lessons/12-claude-agent-sdk-and-hooks/code/main.py","lineNumber":115,"sourceCode":"            reason = event.get(\"stop_reason\")\n            if not isinstance(reason, dict) or reason.get(\"type\") not in {\"requires_action\", \"end_turn\"}:\n                raise EventStreamError(\"idle status needs a supported stop_reason\")\n            stop_reason = reason[\"type\"]\n            if stop_reason == \"requires_action\":\n                event_ids = reason.get(\"event_ids\")\n                if not isinstance(event_ids, list) or not event_ids or not all(\n                    isinstance(item, str) and item in actionable for item in event_ids\n                ):\n                    raise EventStreamError(\"requires_action must reference known actionable event IDs\")\n                pending = set(event_ids)\n            else:\n                pending.clear()\n        elif event_type == \"session.status_terminated\":\n            status = \"terminated\"\n            stop_reason = \"terminated\"\n            pending.clear()\n        elif event_type == \"session.error\":\n            raise EventStreamError(\"session emitted an error event\")\n\n    complete = status == \"terminated\" or (status == \"idle\" and stop_reason == \"end_turn\")\n    return EventSummary(\n        status=status,\n        stop_reason=stop_reason,\n        messages=messages,\n        pending_action_ids=sorted(pending),\n        last_event_id=last_event_id,\n        preview_text=\"\".join(preview_parts),\n        complete=complete,\n    )\n\n\ndef validate_policy(policy: dict[str, Any]) -> list[str]:\n    errors: list[str] = []\n    tools = policy.get(\"tools\")\n    if not isinstance(tools, list) or not tools:\n        return [\"tools must be non-empty\"]","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/certifications/claude/lessons/12-claude-agent-sdk-and-hooks/code/main.py#L97-L133","documentation":"Error \"session emitted an error event\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at certifications/claude/lessons/12-claude-agent-sdk-and-hooks/code/main.py:115 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}