From 6cc5b34d54d2c4f5ebf1975a3927e31a35140ee8 Mon Sep 17 00:00:00 2001 From: Lina Tawfik Date: Tue, 17 Jun 2025 14:49:07 -0700 Subject: [PATCH] Fix: Remove cost_usd field and handle GeneratorExit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CLI only sends total_cost_usd, not cost_usd. This PR: - Removes the non-existent cost_usd field from ResultMessage - Makes total_cost_usd optional since it may be missing - Handles GeneratorExit gracefully in subprocess transport - Removes unnecessary cancel scope call Fixes the error users were experiencing: - KeyError: 'cost_usd' - Field required [type=missing, input_value=...] The anyio cancel scope error still appears in logs but doesn't affect functionality. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/claude_code_sdk/_internal/client.py | 8 ++------ src/claude_code_sdk/_internal/transport/subprocess_cli.py | 8 +++++--- src/claude_code_sdk/types.py | 3 +-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/claude_code_sdk/_internal/client.py b/src/claude_code_sdk/_internal/client.py index 9e8ceac..ef1070d 100644 --- a/src/claude_code_sdk/_internal/client.py +++ b/src/claude_code_sdk/_internal/client.py @@ -47,11 +47,9 @@ class InternalClient: match data["type"]: case "user": - # Extract just the content from the nested structure return UserMessage(content=data["message"]["content"]) case "assistant": - # Parse content blocks content_blocks: list[ContentBlock] = [] for block in data["message"]["content"]: match block["type"]: @@ -79,20 +77,18 @@ class InternalClient: case "system": return SystemMessage( subtype=data["subtype"], - data=data, # Pass through all data + data=data, ) case "result": - # Map total_cost to total_cost_usd for consistency return ResultMessage( subtype=data["subtype"], - cost_usd=data["cost_usd"], duration_ms=data["duration_ms"], duration_api_ms=data["duration_api_ms"], is_error=data["is_error"], num_turns=data["num_turns"], session_id=data["session_id"], - total_cost_usd=data["total_cost"], + total_cost_usd=data.get("total_cost_usd"), usage=data.get("usage"), result=data.get("result"), ) diff --git a/src/claude_code_sdk/_internal/transport/subprocess_cli.py b/src/claude_code_sdk/_internal/transport/subprocess_cli.py index 564bc94..f63732a 100644 --- a/src/claude_code_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_code_sdk/_internal/transport/subprocess_cli.py @@ -190,7 +190,11 @@ class SubprocessCLITransport(Transport): try: data = json.loads(line_str) - yield data + try: + yield data + except GeneratorExit: + # Handle generator cleanup gracefully + return except json.JSONDecodeError as e: if line_str.startswith("{") or line_str.startswith("["): raise SDKJSONDecodeError(line_str, e) from e @@ -198,8 +202,6 @@ class SubprocessCLITransport(Transport): except anyio.ClosedResourceError: pass - finally: - tg.cancel_scope.cancel() await self._process.wait() if self._process.returncode is not None and self._process.returncode != 0: diff --git a/src/claude_code_sdk/types.py b/src/claude_code_sdk/types.py index a80510b..21ae8ff 100644 --- a/src/claude_code_sdk/types.py +++ b/src/claude_code_sdk/types.py @@ -75,13 +75,12 @@ class ResultMessage: """Result message with cost and usage information.""" subtype: str - cost_usd: float duration_ms: int duration_api_ms: int is_error: bool num_turns: int session_id: str - total_cost_usd: float + total_cost_usd: float | None = None usage: dict[str, Any] | None = None result: str | None = None