Skip to content

Sync Cohere client blocks async event loop on every LLM call #1

Closed

Description

@DeFiVC

What

The CohereClient class at src/services/cohere_client.py:23 instantiates cohere.ClientV2 (the synchronous Cohere SDK client). All methods — generate(), generate_json(), and embed() — are declared async and awaited by callers, but the underlying .chat() and .embed() calls execute synchronously on the main asyncio event loop thread.

Why

Every LLM/embedding call freezes the entire FastAPI server for the duration of the Cohere API request (typically 1-10 seconds). Under concurrent requests, the server becomes effectively single-threaded — all other requests queue behind the blocking call. This makes the service unable to handle concurrent users and defeats the purpose of using FastAPI/async.

Scope

In scope:

  • Replace cohere.ClientV2 with cohere.AsyncClientV2 in the Cohere client
  • Update all method calls to use await on the async client methods
  • Verify the async client has the same API surface

Out of scope:

  • Changing the prompt structure or LLM parameters
  • Adding caching or rate limiting
  • Changing the FastAPI application structure

Acceptance Criteria

  • CohereClient uses cohere.AsyncClientV2 instead of cohere.ClientV2
  • All generate(), generate_json(), and embed() methods are truly async
  • The FastAPI server can handle concurrent LLM requests without blocking
  • ruff check src/ tests/ passes
  • pytest passes

Technical Context

File to modify:

  • src/services/cohere_client.py — class CohereClient (line 23)

Current code:

self._client = cohere.ClientV2(api_key=self._api_key)

Fix:

self._client = cohere.AsyncClientV2(api_key=self._api_key)

The AsyncClientV2 has the same method signatures but returns awaitable coroutines. All callers already await the results, so no caller changes should be needed.

Callers:

  • src/services/generation.py — course generation
  • src/services/quiz_generator.py — quiz generation
  • src/services/feedback_engine.py — feedback generation
  • src/services/embedding.py — embedding generation
  • scripts/build_index.py — knowledge base index builder

Activity

  1. added
    bugSomething isn't working
    help wantedExtra attention is needed
    on Jul 20, 2026
  2. Aycode01 commented on Jul 20, 2026

    @Aycode01

    can i work on this

  3. Adamsonoladipupo commented on Jul 20, 2026

    @Adamsonoladipupo

    Can I work on this please?

  4. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  5. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  6. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  7. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  8. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  9. Okorie2000-code commented on Jul 20, 2026

    @Okorie2000-code

    Hi maintainer,
    would love to work on this issue, i have already reviewed the description and will submit a clean PR.
    kindly assign.

  10. Joeloo1 commented on Jul 22, 2026

    @Joeloo1
    Contributor

    Hi maintainer,

    I'd like to work on this issue.

    My approach is to replace cohere.ClientV2 with cohere.AsyncClientV2, update the client methods to use await for all Cohere API calls, and verify that the async API remains compatible with the existing implementation. I'll also validate that concurrent requests no longer block the FastAPI event loop, ensure all affected callers continue to work correctly, and run ruff check and pytest before submitting the PR.

    I'd appreciate it if this issue could be assigned to me. Thanks!

  11. grantfox-oss commented on Jul 22, 2026

    @grantfox-oss

    🦊 GrantFox — @Joeloo1 has been assigned to this issue as part of the Official Campaign | FWC26 campaign!

    Next steps:

    1. Open a Pull Request referencing this issue (e.g., Closes #1)
    2. Your PR will be reviewed by the ChainLearnOfficial maintainers

    Good luck! Track your progress on GrantFox.

  12. grantfox-oss commented on Jul 24, 2026

    @grantfox-oss

    🎉 This issue has been marked as completed on GrantFox as part of the Official Campaign | FWC26 campaign!

    @Joeloo1's PR #15 was approved and merged by @DeFiVC.

    🏆 @Joeloo1: You earned 40 FoxPoints for this contribution! Your current tier: Builder (1,212 total points). Track your full progress on GrantFox.

    👏 Great work, @Joeloo1! Keep contributing to ChainLearnOfficial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26bugSomething isn't workinghelp wantedExtra attention is needed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions