claude-code-sdk-python/test_fix.py
unclecode 18e5899d33 Fix KeyError: 'cost_usd' for Max subscription users
This fix addresses issue #2 where Max subscription users encounter a KeyError
when the API response doesn't include the 'cost_usd' field.

Changes:
- Modified ResultMessage dataclass to make cost_usd and total_cost_usd optional (float | None)
- Updated client.py to use .get() with None default for cost-related fields
- Maintains backward compatibility for users with cost data

This ensures the SDK works for all subscription types, including Max subscriptions
that don't have access to cost information.

By: Unclecode <https://github.com/unclecode>
2025-06-17 15:59:04 +08:00

25 lines
No EOL
695 B
Python

#!/usr/bin/env python3
"""Test script to verify the cost_usd fix works."""
import anyio
from claude_code_sdk import process_query, ClaudeCodeOptions
async def main():
"""Test the SDK with a simple query."""
print("Testing claude-code-sdk with cost_usd fix...")
try:
# Simple test query
async for message in process_query(
"What is 2+2?",
ClaudeCodeOptions()
):
print(f"Message type: {type(message).__name__}")
print(f"Message: {message}")
print("-" * 40)
except Exception as e:
print(f"Error: {type(e).__name__}: {e}")
raise
if __name__ == "__main__":
anyio.run(main)