Skip to content

Does SimpleOpenAI support Virtual Threads? How to use it with structured concurrency in Java 21 #363

@EduardoAzaldegui

Description

@EduardoAzaldegui

Hi maintainers,

I'm evaluating using SimpleOpenAI in a high-concurrency, low-latency application running on Java 21+, and I want to leverage Virtual Threads for better scalability and simpler code structure.

However, I noticed that all public methods return CompletableFuture<T> — which suggests an async/callback-based model. This works fine, but it doesn't automatically benefit from Virtual Threads, since CompletableFuture callbacks typically run on the common fork-join pool or a fixed executor, not on virtual threads.

My Questions:

  1. Is there a synchronous API available?
    For example: String chatCompletion(String prompt) instead of CompletableFuture<String> chatCompletionAsync(...).
    → If yes, I can safely call it inside a Thread.startVirtualThread(...) or StructuredTaskScope.

  2. If only async APIs exist:

    • Are the underlying HTTP calls (e.g., via HttpClient) configured to be compatible with Virtual Threads?
    • Can I safely .join() the CompletableFuture inside a Virtual Thread without blocking real threads?
  3. Best practices for using this library with Virtual Threads:
    Could you provide an example of how to properly integrate SimpleOpenAI into a virtual-threaded workflow?

  4. Future plans:
    Are there any plans to add native Virtual Thread support (e.g., sync methods, or integration with StructuredTaskScope)?


Why this matters:

With Virtual Threads, we can write simple, blocking-style code while achieving massive concurrency. But if the library internally blocks carrier threads or uses non-VT-friendly executors, we lose those benefits.

Thanks for your time and for maintaining this great library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions