Skip to content

Commit 9b1387a

Browse files
authored
Merge pull request #17 from anthropics/fix-optional-cost-fields
Fix: Remove unused cost_usd field
2 parents 54bff2e + b3448f4 commit 9b1387a

File tree

6 files changed

+13
-20
lines changed

6 files changed

+13
-20
lines changed

src/claude_code_sdk/_internal/client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:
4747

4848
match data["type"]:
4949
case "user":
50-
# Extract just the content from the nested structure
5150
return UserMessage(content=data["message"]["content"])
5251

5352
case "assistant":
54-
# Parse content blocks
5553
content_blocks: list[ContentBlock] = []
5654
for block in data["message"]["content"]:
5755
match block["type"]:
@@ -79,20 +77,18 @@ def _parse_message(self, data: dict[str, Any]) -> Message | None:
7977
case "system":
8078
return SystemMessage(
8179
subtype=data["subtype"],
82-
data=data, # Pass through all data
80+
data=data,
8381
)
8482

8583
case "result":
86-
# Map total_cost to total_cost_usd for consistency
8784
return ResultMessage(
8885
subtype=data["subtype"],
89-
cost_usd=data["cost_usd"],
9086
duration_ms=data["duration_ms"],
9187
duration_api_ms=data["duration_api_ms"],
9288
is_error=data["is_error"],
9389
num_turns=data["num_turns"],
9490
session_id=data["session_id"],
95-
total_cost_usd=data["total_cost"],
91+
total_cost_usd=data.get("total_cost_usd"),
9692
usage=data.get("usage"),
9793
result=data.get("result"),
9894
)

src/claude_code_sdk/_internal/transport/subprocess_cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,18 @@ async def read_stderr() -> None:
190190

191191
try:
192192
data = json.loads(line_str)
193-
yield data
193+
try:
194+
yield data
195+
except GeneratorExit:
196+
# Handle generator cleanup gracefully
197+
return
194198
except json.JSONDecodeError as e:
195199
if line_str.startswith("{") or line_str.startswith("["):
196200
raise SDKJSONDecodeError(line_str, e) from e
197201
continue
198202

199203
except anyio.ClosedResourceError:
200204
pass
201-
finally:
202-
tg.cancel_scope.cancel()
203205

204206
await self._process.wait()
205207
if self._process.returncode is not None and self._process.returncode != 0:

src/claude_code_sdk/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ class ResultMessage:
7575
"""Result message with cost and usage information."""
7676

7777
subtype: str
78-
cost_usd: float
7978
duration_ms: int
8079
duration_api_ms: int
8180
is_error: bool
8281
num_turns: int
8382
session_id: str
84-
total_cost_usd: float
83+
total_cost_usd: float | None = None
8584
usage: dict[str, Any] | None = None
8685
result: str | None = None
8786

tests/test_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ async def mock_receive():
8888
yield {
8989
"type": "result",
9090
"subtype": "success",
91-
"cost_usd": 0.001,
9291
"duration_ms": 1000,
9392
"duration_api_ms": 800,
9493
"is_error": False,
9594
"num_turns": 1,
9695
"session_id": "test-session",
97-
"total_cost": 0.001,
96+
"total_cost_usd": 0.001,
9897
}
9998

10099
mock_transport.receive_messages = mock_receive

tests/test_integration.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ async def mock_receive():
4343
yield {
4444
"type": "result",
4545
"subtype": "success",
46-
"cost_usd": 0.001,
4746
"duration_ms": 1000,
4847
"duration_api_ms": 800,
4948
"is_error": False,
5049
"num_turns": 1,
5150
"session_id": "test-session",
52-
"total_cost": 0.001,
51+
"total_cost_usd": 0.001,
5352
}
5453

5554
mock_transport.receive_messages = mock_receive
@@ -71,7 +70,7 @@ async def mock_receive():
7170

7271
# Check result message
7372
assert isinstance(messages[1], ResultMessage)
74-
assert messages[1].cost_usd == 0.001
73+
assert messages[1].total_cost_usd == 0.001
7574
assert messages[1].session_id == "test-session"
7675

7776
anyio.run(_test)
@@ -109,13 +108,12 @@ async def mock_receive():
109108
yield {
110109
"type": "result",
111110
"subtype": "success",
112-
"cost_usd": 0.002,
113111
"duration_ms": 1500,
114112
"duration_api_ms": 1200,
115113
"is_error": False,
116114
"num_turns": 1,
117115
"session_id": "test-session-2",
118-
"total_cost": 0.002,
116+
"total_cost_usd": 0.002,
119117
}
120118

121119
mock_transport.receive_messages = mock_receive

tests/test_types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def test_result_message(self):
4545
"""Test creating a ResultMessage."""
4646
msg = ResultMessage(
4747
subtype="success",
48-
cost_usd=0.01,
4948
duration_ms=1500,
5049
duration_api_ms=1200,
5150
is_error=False,
@@ -54,7 +53,7 @@ def test_result_message(self):
5453
total_cost_usd=0.01,
5554
)
5655
assert msg.subtype == "success"
57-
assert msg.cost_usd == 0.01
56+
assert msg.total_cost_usd == 0.01
5857
assert msg.session_id == "session-123"
5958

6059

0 commit comments

Comments
 (0)