-
Notifications
You must be signed in to change notification settings - Fork 303
fix(json-rpc): add stream_send_timeout for JSON-RPC streaming endpoints #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
07c1f94
9228299
4d54c28
c7c39ba
17cf975
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -172,38 +172,43 @@ | |||||||||||||||||
| for model in A2ARequestModel.__args__ | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| def __init__( # noqa: PLR0913 | ||||||||||||||||||
| self, | ||||||||||||||||||
| agent_card: AgentCard, | ||||||||||||||||||
| http_handler: RequestHandler, | ||||||||||||||||||
| extended_agent_card: AgentCard | None = None, | ||||||||||||||||||
| context_builder: CallContextBuilder | None = None, | ||||||||||||||||||
| card_modifier: Callable[[AgentCard], AgentCard] | None = None, | ||||||||||||||||||
| extended_card_modifier: Callable[ | ||||||||||||||||||
| [AgentCard, ServerCallContext], AgentCard | ||||||||||||||||||
| ] | ||||||||||||||||||
| | None = None, | ||||||||||||||||||
| max_content_length: int | None = 10 * 1024 * 1024, # 10MB | ||||||||||||||||||
| stream_send_timeout: float | None = None, | ||||||||||||||||||
| ) -> None: | ||||||||||||||||||
| """Initializes the JSONRPCApplication. | ||||||||||||||||||
|
|
||||||||||||||||||
| Args: | ||||||||||||||||||
| agent_card: The AgentCard describing the agent's capabilities. | ||||||||||||||||||
| http_handler: The handler instance responsible for processing A2A | ||||||||||||||||||
| requests via http. | ||||||||||||||||||
| extended_agent_card: An optional, distinct AgentCard to be served | ||||||||||||||||||
| at the authenticated extended card endpoint. | ||||||||||||||||||
| context_builder: The CallContextBuilder used to construct the | ||||||||||||||||||
| ServerCallContext passed to the http_handler. If None, no | ||||||||||||||||||
| ServerCallContext is passed. | ||||||||||||||||||
| card_modifier: An optional callback to dynamically modify the public | ||||||||||||||||||
| agent card before it is served. | ||||||||||||||||||
| extended_card_modifier: An optional callback to dynamically modify | ||||||||||||||||||
| the extended agent card before it is served. It receives the | ||||||||||||||||||
| call context. | ||||||||||||||||||
| max_content_length: The maximum allowed content length for incoming | ||||||||||||||||||
| requests. Defaults to 10MB. Set to None for unbounded maximum. | ||||||||||||||||||
| stream_send_timeout: The timeout in seconds for sending events in | ||||||||||||||||||
| streaming responses. Defaults to None, which uses Starlette's | ||||||||||||||||||
| default timeout. Set to a larger value or None to disable for | ||||||||||||||||||
| long-running agents. | ||||||||||||||||||
|
Comment on lines
544
to
556
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring is contradictory. It first says
Suggested change
|
||||||||||||||||||
| """ | ||||||||||||||||||
| if not _package_starlette_installed: | ||||||||||||||||||
| raise ImportError( | ||||||||||||||||||
| 'Packages `starlette` and `sse-starlette` are required to use the' | ||||||||||||||||||
|
|
@@ -222,6 +227,7 @@ | |||||||||||||||||
| ) | ||||||||||||||||||
| self._context_builder = context_builder or DefaultCallContextBuilder() | ||||||||||||||||||
| self._max_content_length = max_content_length | ||||||||||||||||||
| self.stream_send_timeout = stream_send_timeout | ||||||||||||||||||
|
|
||||||||||||||||||
| def _generate_error_response( | ||||||||||||||||||
| self, request_id: str | int | None, error: JSONRPCError | A2AError | ||||||||||||||||||
|
|
@@ -540,8 +546,14 @@ | |||||||||||||||||
| async for item in stream: | ||||||||||||||||||
| yield {'data': item.root.model_dump_json(exclude_none=True)} | ||||||||||||||||||
|
|
||||||||||||||||||
| send_timeout = context.state.get( | ||||||||||||||||||
| 'stream_send_timeout', self.stream_send_timeout | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| return EventSourceResponse( | ||||||||||||||||||
| event_generator(handler_result), headers=headers | ||||||||||||||||||
| event_generator(handler_result), | ||||||||||||||||||
| headers=headers, | ||||||||||||||||||
| send_timeout=send_timeout, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
||||||||||||||||||
| if isinstance(handler_result, JSONRPCErrorResponse): | ||||||||||||||||||
| return JSONResponse( | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,18 +48,19 @@ | |||||||||||||||||
| (SSE). | ||||||||||||||||||
| """ | ||||||||||||||||||
|
|
||||||||||||||||||
| def __init__( # noqa: PLR0913 | ||||||||||||||||||
| self, | ||||||||||||||||||
| agent_card: AgentCard, | ||||||||||||||||||
| http_handler: RequestHandler, | ||||||||||||||||||
| extended_agent_card: AgentCard | None = None, | ||||||||||||||||||
| context_builder: CallContextBuilder | None = None, | ||||||||||||||||||
| card_modifier: Callable[[AgentCard], AgentCard] | None = None, | ||||||||||||||||||
| extended_card_modifier: Callable[ | ||||||||||||||||||
| [AgentCard, ServerCallContext], AgentCard | ||||||||||||||||||
| ] | ||||||||||||||||||
| | None = None, | ||||||||||||||||||
| max_content_length: int | None = 10 * 1024 * 1024, # 10MB | ||||||||||||||||||
| stream_send_timeout: float | None = None, | ||||||||||||||||||
| ) -> None: | ||||||||||||||||||
| """Initializes the A2AStarletteApplication. | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -79,6 +80,10 @@ | |||||||||||||||||
| call context. | ||||||||||||||||||
| max_content_length: The maximum allowed content length for incoming | ||||||||||||||||||
| requests. Defaults to 10MB. Set to None for unbounded maximum. | ||||||||||||||||||
| stream_send_timeout: The timeout in seconds for sending events in | ||||||||||||||||||
| streaming responses. Defaults to None, which uses Starlette's | ||||||||||||||||||
| default timeout. Set to a larger value or None to disable for | ||||||||||||||||||
| long-running agents. | ||||||||||||||||||
|
||||||||||||||||||
| stream_send_timeout: The timeout in seconds for sending events in | |
| streaming responses. Defaults to None, which uses Starlette's | |
| default timeout. Set to a larger value or None to disable for | |
| long-running agents. | |
| stream_send_timeout: The timeout in seconds for sending events in | |
| streaming responses. Defaults to `None`, which disables the timeout. | |
| This changes the default behavior from using Starlette's 5-second | |
| default. Set a float value to specify a timeout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phrase 'in default timeout' is unclear. The docstring should also clarify that
Nonedisables the timeout, which is a change in the default behavior. For clarity and consistency, could you update it?