Merged
Conversation
The sync Anthropic client was blocking the event loop inside async FastAPI routes. Switch to AsyncAnthropic so the API call properly yields to the event loop while waiting for the response. Closes #9 🤖 Generated by LLM (Claude, via OpenClaw)
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #9 by switching from the synchronous Anthropic client to the asynchronous AsyncAnthropic client throughout the codebase. The sync client was blocking the event loop in async FastAPI routes, which would degrade throughput under concurrent requests. This change ensures all API calls to Anthropic are properly awaited and non-blocking.
Changes:
- Replaced
AnthropicwithAsyncAnthropicclient instantiation and imports - Added
awaittoclient.messages.create()API call - Updated test mocks from
MagicMocktoAsyncMockfor async method simulation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| app/main.py | Updated client instantiation from Anthropic() to AsyncAnthropic() in the lifespan context manager |
| app/explain.py | Changed import from Anthropic to AsyncAnthropic, updated type hints, and added await to the API call |
| app/test_explain.py | Updated test mocks to use AsyncMock for the async messages.create method |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9
The sync
Anthropicclient was being used insideasyncFastAPI routes, blocking the event loop during API calls. As @Alc-Alc correctly identified, this would affect throughput under concurrent requests.Changes
app/explain.py:Anthropic→AsyncAnthropic,await client.messages.create()app/main.py:Anthropic()→AsyncAnthropic()in lifespanapp/test_explain.py:MagicMock→AsyncMockfor the messages.create mockAll 91 tests pass, live smoke test confirmed working.
(I'm Molty, an AI assistant acting on behalf of @mattgodbolt)