55
66from abc import ABC , abstractmethod
77from collections .abc import AsyncGenerator , Callable
8- from typing import Any
8+ from typing import TYPE_CHECKING , Any
99
10- from fastapi import FastAPI
1110from pydantic import ValidationError
12- from sse_starlette .sse import EventSourceResponse
13- from starlette .applications import Starlette
14- from starlette .authentication import BaseUser
15- from starlette .exceptions import HTTPException
16- from starlette .requests import Request
17- from starlette .responses import JSONResponse , Response
18- from starlette .status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
1911
2012from a2a .auth .user import UnauthenticatedUser
2113from a2a .auth .user import User as A2AUser
6153
6254logger = logging .getLogger (__name__ )
6355
56+ if TYPE_CHECKING :
57+ from fastapi import FastAPI
58+ from sse_starlette .sse import EventSourceResponse
59+ from starlette .applications import Starlette
60+ from starlette .authentication import BaseUser
61+ from starlette .exceptions import HTTPException
62+ from starlette .requests import Request
63+ from starlette .responses import JSONResponse , Response
64+ from starlette .status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
65+
66+ _package_starlette_installed = True
67+ else :
68+ FastAPI = Any
69+ try :
70+ from sse_starlette .sse import EventSourceResponse
71+ from starlette .applications import Starlette
72+ from starlette .authentication import BaseUser
73+ from starlette .exceptions import HTTPException
74+ from starlette .requests import Request
75+ from starlette .responses import JSONResponse , Response
76+ from starlette .status import HTTP_413_REQUEST_ENTITY_TOO_LARGE
77+
78+ _package_starlette_installed = True
79+ except ImportError :
80+ _package_starlette_installed = False
81+ # Provide placeholder types for runtime type hinting when dependencies are not installed.
82+ # These will not be used if the code path that needs them is guarded by _http_server_installed.
83+ EventSourceResponse = Any
84+ Starlette = Any
85+ BaseUser = Any
86+ HTTPException = Any
87+ Request = Any
88+ JSONResponse = Any
89+ Response = Any
90+ HTTP_413_REQUEST_ENTITY_TOO_LARGE = Any
91+
6492
6593class StarletteUserProxy (A2AUser ):
6694 """Adapts the Starlette User class to the A2A user representation."""
@@ -135,7 +163,7 @@ def __init__( # noqa: PLR0913
135163 ]
136164 | None = None ,
137165 ) -> None :
138- """Initializes the A2AStarletteApplication .
166+ """Initializes the JSONRPCApplication .
139167
140168 Args:
141169 agent_card: The AgentCard describing the agent's capabilities.
@@ -152,6 +180,12 @@ def __init__( # noqa: PLR0913
152180 the extended agent card before it is served. It receives the
153181 call context.
154182 """
183+ if not _package_starlette_installed :
184+ raise ImportError (
185+ 'Packages `starlette` and `sse-starlette` are required to use the'
186+ ' `JSONRPCApplication`. They can be added as a part of `a2a-sdk`'
187+ ' optional dependencies, `a2a-sdk[http-server]`.'
188+ )
155189 self .agent_card = agent_card
156190 self .extended_agent_card = extended_agent_card
157191 self .card_modifier = card_modifier
0 commit comments