-
Notifications
You must be signed in to change notification settings - Fork 0
feat(agents): migrate from Anthropic Python SDK to Claude Agent SDK #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Replace the `anthropic` Python SDK with `claude-code-sdk` to enable automatic authentication inheritance from Claude Code CLI. Key changes: - Add new utils/agent_sdk.py module with generate_structured_response() and invoke_claude_code() wrapper functions - Update DesignAgent to use Agent SDK for concept/GDD/tech spec generation - Update PublishAgent to use Agent SDK for store page content generation - Update BuildAgent to use native Agent SDK instead of subprocess-based ClaudeCodeRunner - Make API key optional in BaseAgent (SDK handles auth automatically) - Update all tests to use new mocking patterns for Agent SDK - Update documentation with new authentication options Benefits: - Subscription support (Pro/Max): Run `claude` once to login, SDK uses it automatically - API Key support: Set ANTHROPIC_API_KEY, works as before - No explicit configuration needed - SDK handles auth priority Closes #37 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary of ChangesHello @PSthelyBlog, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request undertakes a significant refactoring by migrating the project's AI agent interactions from the legacy Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request successfully migrates the project from the Anthropic Python SDK to the Claude Agent SDK, enabling automatic authentication inheritance and simplifying API calls. The changes are well-implemented across agents (Design, Build, Publish) and documentation, with corresponding updates to unit tests. The new agent_sdk.py module provides clear wrapper functions for structured generation and agentic tasks. However, there are two high severity issues identified in the new src/game_workflow/utils/agent_sdk.py file regarding error handling for ResultMessage objects, which could lead to less specific error reporting or false positives for success. These should be addressed to ensure robust error handling as outlined in the migration plan.
| async for message in query( | ||
| prompt=full_prompt, | ||
| options=ClaudeCodeOptions( | ||
| model=model, | ||
| max_turns=1, # Single turn for generation tasks | ||
| ), | ||
| ): | ||
| # Handle different message types | ||
| if hasattr(message, "content"): | ||
| for block in message.content: | ||
| if hasattr(block, "text"): | ||
| text_parts.append(block.text) | ||
|
|
||
| result = "".join(text_parts) | ||
| if not result: | ||
| raise RuntimeError("Agent SDK returned empty response") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generate_structured_response function should explicitly handle ResultMessage errors from the Agent SDK. The current implementation only checks if the final result is empty, which might mask specific errors reported by the SDK. Implementing a check for message.is_error will provide more precise error reporting, as outlined in the migration plan.
| async for message in query( | |
| prompt=full_prompt, | |
| options=ClaudeCodeOptions( | |
| model=model, | |
| max_turns=1, # Single turn for generation tasks | |
| ), | |
| ): | |
| # Handle different message types | |
| if hasattr(message, "content"): | |
| for block in message.content: | |
| if hasattr(block, "text"): | |
| text_parts.append(block.text) | |
| result = "".join(text_parts) | |
| if not result: | |
| raise RuntimeError("Agent SDK returned empty response") | |
| async for message in query( | |
| prompt=full_prompt, | |
| options=ClaudeCodeOptions( | |
| model=model, | |
| max_turns=1, # Single turn for generation tasks | |
| ), | |
| ): | |
| if hasattr(message, "content"): | |
| for block in message.content: | |
| if hasattr(block, "text"): | |
| text_parts.append(block.text) | |
| # Check for errors if the SDK yields messages with an 'is_error' attribute | |
| if hasattr(message, "is_error") and message.is_error: | |
| raise RuntimeError(f"Agent SDK error: {message}") | |
| result = "".join(text_parts) | |
| if not result: | |
| raise RuntimeError("Agent SDK returned empty response or an unhandled error occurred.") |
| async for message in query(prompt=full_prompt, options=options): | ||
| # Collect text output from messages | ||
| if hasattr(message, "content"): | ||
| for block in message.content: | ||
| if hasattr(block, "text"): | ||
| output_parts.append(block.text) | ||
| logger.debug(f"claude-code: {block.text[:100]}...") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The invoke_claude_code function currently marks the operation as success=True unless an exception is caught during the query iteration. It should also explicitly check for ResultMessage errors within the async for loop, as suggested in the migration plan, to accurately reflect the outcome of the Claude Code execution. If a ResultMessage indicates an error, success should be set to False and the error_message captured.
| async for message in query(prompt=full_prompt, options=options): | |
| # Collect text output from messages | |
| if hasattr(message, "content"): | |
| for block in message.content: | |
| if hasattr(block, "text"): | |
| output_parts.append(block.text) | |
| logger.debug(f"claude-code: {block.text[:100]}...") | |
| async for message in query(prompt=full_prompt, options=options): | |
| # Collect text output from messages | |
| if hasattr(message, "content"): | |
| for block in message.content: | |
| if hasattr(block, "text"): | |
| output_parts.append(block.text) | |
| logger.debug(f"claude-code: {block.text[:100]}...") | |
| # Check for errors if the SDK yields messages with an 'is_error' attribute | |
| if hasattr(message, "is_error") and message.is_error: | |
| error_message = f"Claude Code execution reported an error: {message}" | |
| success = False | |
| break # Exit loop on error |
- Mark migration-plan.md as complete with PR #38 reference - Add completion summary with implementation details - Add Session 17 to notes.md documenting the migration work Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Summary
anthropicPython SDK withclaude-code-sdkto enable automatic authentication inheritance from Claude Code CLIutils/agent_sdk.pymodule with wrapper functions for Agent SDKKey Changes
New Authentication Options
claudeonce to login → SDK uses it automaticallyANTHROPIC_API_KEY→ works as beforeNo explicit configuration needed - the SDK handles auth priority automatically.
Files Changed
pyproject.toml- Replaceanthropic>=0.40.0withclaude-code-sdk>=0.0.10src/game_workflow/utils/agent_sdk.py- NEW SDK wrapper utilitiessrc/game_workflow/agents/design.py- Use Agent SDK for generationsrc/game_workflow/agents/publish.py- Use Agent SDK for generationsrc/game_workflow/agents/build.py- Use native Agent SDK instead of subprocesssrc/game_workflow/agents/base.py- Make API key optional with deprecation warningREADME.mdanddocs/setup.mdupdated with new auth instructionsTest plan
pytest tests/unit -v(337 passed)ruff check .ruff format --check .Closes #37
🤖 Generated with Claude Code