can RPC methods be async on the client side?
# ask-ai
a
can RPC methods be async on the client side?
t
Thanks for reaching out, Sebastián. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes — RPC methods on the client can be asynchronous. You can register an RPC handler that performs async work (awaits promises/futures or returns a coroutine) and then returns the result when complete. Callers use the async
performRpc
(or equivalent) and receive the resolved value when the handler finishes. If you’d like, tell me which SDK you’re using (JavaScript, Python, Swift, Kotlin, Go) and I’ll show a short async example for that SDK. Sources: Remote method calls | LiveKit docs
a
python sdk
t
Regarding Python: yes — RPC handlers can be async. Example using the Python SDK:
Copy code
from livekit import Room, RpcInvocationData

room = Room(...)

@room.local_participant.register_rpc_method("greet")
async def handle_greet(data: RpcInvocationData):
    # async work: database call, HTTP request, etc.
    user = data.caller_identity
    payload = data.payload
    # await some async operation, e.g. await fetch_user_info(user)
    await some_async_operation(payload)
    return f"Hello, {user}!"
Calling an RPC from a client is also async and returns the handler's resolved value (use
await
when calling). If you want, share a short use case (e.g., calling an external API inside the handler) and I’ll provide a concrete snippet.
a
and what if the tool can be slow, is there any limit on rpc timeout?
t
Regarding RPC timeouts in the Python SDK: RPC calls have a configurable response timeout. The default is 10 seconds; if the handler doesn't return within the timeout the caller receives a 1502 RESPONSE_TIMEOUT error. You can increase the timeout when calling
perform_rpc
, for example:
Copy code
response = await room.local_participant.perform_rpc(
    destination_identity=participant_identity,
    method="slow_operation",
    payload="...",
    response_timeout=30.0,  # wait up to 30 seconds
)
Best options for slow operations: • Raise
response_timeout
to match expected duration. • Have the RPC start the long task and return immediately (e.g., "task started"), then notify completion via events or polling. • If using speech-generation flows, consider
allow_interruptions=False
where relevant. Would you like a concrete example for starting a background task and notifying completion? Sources: Remote method calls | LiveKit docs | Agent speech and audio | LiveKit docs
a
but if it's a tool call I need the result
t
If you must get the tool's result synchronously via RPC, you have two practical options: 1) Increase the RPC
response_timeout
when calling
perform_rpc
so it comfortably exceeds the tool's worst-case runtime.
Copy code
response = await room.local_participant.perform_rpc(
    destination_identity=participant_identity,
    method="run_tool",
    payload=payload,
    response_timeout=120.0,  # e.g., wait up to 2 minutes
)
2) If the tool can be very slow/unpredictable, keep the RPC handler responsive while still returning the final result by running the long task inside the handler but with careful resource/timeout management: • Ensure the handler actually awaits the long-running async call so the final result is returned before the timeout. • Consider increasing
response_timeout
enough for the full operation. • Make the handler robust to errors and return clear failure responses (so callers don't hang waiting). If blocking the caller for the full duration is unacceptable, use a hybrid approach: • RPC returns immediately with a task ID (acknowledge). • Worker runs the long tool and, when done, delivers the result via a LiveKit event/message or a follow-up RPC the caller can await/poll. Which approach do you prefer? I can provide a concrete Python snippet for a handler that awaits a long async tool call (with error handling) or an example of the task-ID + event notification pattern.