Skip to content

Commit 07c1f94

Browse files
committed
fix(jsonrpc): add steam_send_timeout param for configurable SSE timeouts to JSONRPCApplication, A2AStarletteApplication, and A2AFastAPIApplication to resolve timeout issues
1 parent ba142df commit 07c1f94

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def __init__( # noqa: PLR0913
7878
]
7979
| None = None,
8080
max_content_length: int | None = 10 * 1024 * 1024, # 10MB
81+
stream_send_timeout: float | None = None,
8182
) -> None:
8283
"""Initializes the A2AFastAPIApplication.
8384
@@ -97,6 +98,8 @@ def __init__( # noqa: PLR0913
9798
call context.
9899
max_content_length: The maximum allowed content length for incoming
99100
requests. Defaults to 10MB. Set to None for unbounded maximum.
101+
stream_send_timeout: The timeout in seconds for sending events in default timeout. Set to a larger value or None to disable for
102+
long-running agents.
100103
"""
101104
if not _package_fastapi_installed:
102105
raise ImportError(
@@ -112,6 +115,7 @@ def __init__( # noqa: PLR0913
112115
card_modifier=card_modifier,
113116
extended_card_modifier=extended_card_modifier,
114117
max_content_length=max_content_length,
118+
stream_send_timeout=stream_send_timeout,
115119
)
116120

117121
def add_routes_to_app(

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def __init__( # noqa: PLR0913
184184
]
185185
| None = None,
186186
max_content_length: int | None = 10 * 1024 * 1024, # 10MB
187+
stream_send_timeout: float | None = None,
187188
) -> None:
188189
"""Initializes the JSONRPCApplication.
189190
@@ -203,6 +204,10 @@ def __init__( # noqa: PLR0913
203204
call context.
204205
max_content_length: The maximum allowed content length for incoming
205206
requests. Defaults to 10MB. Set to None for unbounded maximum.
207+
stream_send_timeout: The timeout in seconds for sending events in
208+
streaming responses. Defaults to None, which uses Starlette's
209+
default timeout. Set to a larger value or None to disable for
210+
long-running agents.
206211
"""
207212
if not _package_starlette_installed:
208213
raise ImportError(
@@ -222,6 +227,7 @@ def __init__( # noqa: PLR0913
222227
)
223228
self._context_builder = context_builder or DefaultCallContextBuilder()
224229
self._max_content_length = max_content_length
230+
self.stream_send_timeout = stream_send_timeout
225231

226232
def _generate_error_response(
227233
self, request_id: str | int | None, error: JSONRPCError | A2AError
@@ -540,8 +546,14 @@ async def event_generator(
540546
async for item in stream:
541547
yield {'data': item.root.model_dump_json(exclude_none=True)}
542548

549+
send_timeout = context.state.get(
550+
'stream_send_timeout', self.stream_send_timeout
551+
)
552+
543553
return EventSourceResponse(
544-
event_generator(handler_result), headers=headers
554+
event_generator(handler_result),
555+
headers=headers,
556+
send_timeout=send_timeout,
545557
)
546558
if isinstance(handler_result, JSONRPCErrorResponse):
547559
return JSONResponse(

src/a2a/server/apps/jsonrpc/starlette_app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__( # noqa: PLR0913
6060
]
6161
| None = None,
6262
max_content_length: int | None = 10 * 1024 * 1024, # 10MB
63+
stream_send_timeout: float | None = None,
6364
) -> None:
6465
"""Initializes the A2AStarletteApplication.
6566
@@ -79,6 +80,10 @@ def __init__( # noqa: PLR0913
7980
call context.
8081
max_content_length: The maximum allowed content length for incoming
8182
requests. Defaults to 10MB. Set to None for unbounded maximum.
83+
stream_send_timeout: The timeout in seconds for sending events in
84+
streaming responses. Defaults to None, which uses Starlette's
85+
default timeout. Set to a larger value or None to disable for
86+
long-running agents.
8287
"""
8388
if not _package_starlette_installed:
8489
raise ImportError(
@@ -94,6 +99,7 @@ def __init__( # noqa: PLR0913
9499
card_modifier=card_modifier,
95100
extended_card_modifier=extended_card_modifier,
96101
max_content_length=max_content_length,
102+
stream_send_timeout=stream_send_timeout,
97103
)
98104

99105
def routes(

0 commit comments

Comments
 (0)