Skip to content

Commit 2d214f1

Browse files
committed
Split starlette and fastapi imports and error msg
Since all three apps A2AStarletteApplication, A2AFastAPIApplication, and JSONRPCApplication are public APIs, we should assume that each can be used direcly by downstream projects. Therefore, proper ImportError messages should be printed when initializing each type of app with missing optional dependencies.
1 parent d1895af commit 2d214f1

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55

66
if TYPE_CHECKING:
77
from fastapi import FastAPI, Request, Response
8+
9+
_package_fastapi_installed = True
810
else:
911
try:
1012
from fastapi import FastAPI, Request, Response
13+
14+
_package_fastapi_installed = True
1115
except ImportError:
1216
FastAPI = Any
1317
Request = Any
1418
Response = Any
1519

20+
_package_fastapi_installed = False
21+
1622
from a2a.server.apps.jsonrpc.jsonrpc_app import (
1723
CallContextBuilder,
1824
JSONRPCApplication,
@@ -51,6 +57,12 @@ def __init__(
5157
ServerCallContext passed to the http_handler. If None, no
5258
ServerCallContext is passed.
5359
"""
60+
if not _package_fastapi_installed:
61+
raise ImportError(
62+
'The `fastapi` package is required to use the `A2AFastAPIApplication`.'
63+
' It can be added as a part of `a2a-sdk` optional dependencies,'
64+
' `a2a-sdk[http-server]`.'
65+
)
5466
super().__init__(
5567
agent_card=agent_card,
5668
http_handler=http_handler,

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,21 @@
4747
from starlette.requests import Request
4848
from starlette.responses import JSONResponse, Response
4949

50-
_http_server_installed = True
50+
_package_starlette_installed = True
5151
else:
52+
FastAPI = Any
5253
try:
53-
from fastapi import FastAPI
5454
from sse_starlette.sse import EventSourceResponse
5555
from starlette.applications import Starlette
5656
from starlette.authentication import BaseUser
5757
from starlette.requests import Request
5858
from starlette.responses import JSONResponse, Response
5959

60-
_http_server_installed = True
60+
_package_starlette_installed = True
6161
except ImportError:
62-
_http_server_installed = False
62+
_package_starlette_installed = False
6363
# Provide placeholder types for runtime type hinting when dependencies are not installed.
6464
# These will not be used if the code path that needs them is guarded by _http_server_installed.
65-
FastAPI = Any
6665
EventSourceResponse = Any
6766
Starlette = Any
6867
BaseUser = Any
@@ -145,9 +144,11 @@ def __init__(
145144
ServerCallContext passed to the http_handler. If None, no
146145
ServerCallContext is passed.
147146
"""
148-
if not _http_server_installed:
147+
if not _package_starlette_installed:
149148
raise ImportError(
150-
'The `a2a-sdk[http-server]` package is required to use the `JSONRPCApplication`.'
149+
'Packages `starlette` and `sse-starlette` are required to use the'
150+
' `JSONRPCApplication`. They can be added as a part of `a2a-sdk`'
151+
' optional dependencies, `a2a-sdk[http-server]`.'
151152
)
152153
self.agent_card = agent_card
153154
self.extended_agent_card = extended_agent_card

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
if TYPE_CHECKING:
77
from starlette.applications import Starlette
88
from starlette.routing import Route
9+
10+
_package_starlette_installed = True
11+
912
else:
1013
try:
1114
from starlette.applications import Starlette
1215
from starlette.routing import Route
16+
17+
_package_starlette_installed = True
1318
except ImportError:
1419
Starlette = Any
1520
Route = Any
1621

22+
_package_starlette_installed = False
23+
1724
from a2a.server.apps.jsonrpc.jsonrpc_app import (
1825
CallContextBuilder,
1926
JSONRPCApplication,
@@ -52,6 +59,12 @@ def __init__(
5259
ServerCallContext passed to the http_handler. If None, no
5360
ServerCallContext is passed.
5461
"""
62+
if not _package_starlette_installed:
63+
raise ImportError(
64+
'Packages `starlette` and `sse-starlette` are required to use the'
65+
' `A2AStarletteApplication`. It can be added as a part of `a2a-sdk`'
66+
' optional dependencies, `a2a-sdk[http-server]`.'
67+
)
5568
super().__init__(
5669
agent_card=agent_card,
5770
http_handler=http_handler,

0 commit comments

Comments
 (0)