Skip to content

feat(functions): add async function invocation - #659

Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
mainfrom
async-invoke-python
Aug 5, 2026
Merged

feat(functions): add async function invocation#659
Abhijeet Prasad (AbhiPrasad) merged 1 commit into
mainfrom
async-invoke-python

Conversation

@AbhiPrasad

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Aug 5, 2026

Copy link
Copy Markdown
Member

resolves https://linear.app/braintrustdata/issue/SDK-140/support-async-version-if-invoke-in-python-sdk

Add invoke_async() with the same configuration and typed stream overloads as invoke(). Non-streaming calls return the decoded result without blocking the event loop; streaming calls return BraintrustStream with async iteration and final_value_async() support.

stream=False is the default. The complete decoded result is returned:

from braintrust import invoke_async

result = await invoke_async(
    project_name="my-project",
    slug="my-function",
    input={"question": "What is 2 + 2?"},
)
async task
    │ await
    ▼
invoke_async()
    │ worker thread
    ▼
invoke() ──HTTP──► Braintrust
    │
    ▼
decoded result

Set stream=True to receive a BraintrustStream supporting async for:

stream = await invoke_async(
    project_name="my-project",
    slug="my-function",
    input={"question": "Tell me a story"},
    stream=True,
)

async for chunk in stream:
    print(chunk)
invoke_async(stream=True)
    │ worker opens SSE request
    ▼
BraintrustStream
    │
    ├── async for
    │      └── worker reads each SSE chunk
    │
    └── await final_value_async()
           └── worker aggregates the stream

Add invoke_async() with the same configuration and typed stream overloads
as invoke(). Non-streaming calls return the decoded result without blocking
the event loop; streaming calls return BraintrustStream with async iteration
and final_value_async() support.

Bridge the existing synchronous HTTP and SSE transports through worker
threads, preserving pending stream reads across caller cancellation. Cover
the API with VCR-backed invocation, stream, cancellation, and type tests.
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit 908f148 into main Aug 5, 2026
82 checks passed
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) deleted the async-invoke-python branch August 5, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants