mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-07 14:45:00 +00:00
Fix KeyError and async cleanup issues
- Fix KeyError for 'cost_usd' by using .get() with fallback to 'total_cost' - Handle GeneratorExit exception during async cleanup - Add try/except for cancel_scope.cancel() to prevent RuntimeError These changes resolve runtime errors when using the SDK with Claude Code CLI.
This commit is contained in:
parent
54bff2e85d
commit
af7dfd0501
2 changed files with 9 additions and 4 deletions
|
@ -86,13 +86,13 @@ class InternalClient:
|
|||
# Map total_cost to total_cost_usd for consistency
|
||||
return ResultMessage(
|
||||
subtype=data["subtype"],
|
||||
cost_usd=data["cost_usd"],
|
||||
cost_usd=data.get("cost_usd", data.get("total_cost", 0.0)),
|
||||
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", 0.0),
|
||||
usage=data.get("usage"),
|
||||
result=data.get("result"),
|
||||
)
|
||||
|
|
|
@ -196,10 +196,15 @@ class SubprocessCLITransport(Transport):
|
|||
raise SDKJSONDecodeError(line_str, e) from e
|
||||
continue
|
||||
|
||||
except anyio.ClosedResourceError:
|
||||
except (anyio.ClosedResourceError, GeneratorExit):
|
||||
pass
|
||||
finally:
|
||||
tg.cancel_scope.cancel()
|
||||
# Cancel the task group gracefully
|
||||
try:
|
||||
tg.cancel_scope.cancel()
|
||||
except RuntimeError:
|
||||
# Ignore errors if cancel scope is already exited
|
||||
pass
|
||||
|
||||
await self._process.wait()
|
||||
if self._process.returncode is not None and self._process.returncode != 0:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue