Skip to content

Commit 6ece7d5

Browse files
committed
feat: Integrate Pydantic v2 compatibility patches into ADK web server
- Import patch_types_for_pydantic_v2 and create_robust_openapi_function - Apply compatibility patches before FastAPI app creation - Replace default OpenAPI generation with robust error-handling version - Add logging for patch application status and OpenAPI integration - Ensures Swagger UI works correctly with Pydantic v2 by handling: * MCP ClientSession schema generation issues * types.GenericAlias compatibility problems * httpx.Client schema generation errors * Recursion errors in complex model schemas This integration enables Swagger UI functionality while maintaining backward compatibility and providing comprehensive error handling.
1 parent a082159 commit 6ece7d5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
from .utils.base_agent_loader import BaseAgentLoader
9595
from .utils.shared_value import SharedValue
9696
from .utils.state import create_empty_state
97+
from ..utils.pydantic_v2_compatibility import patch_types_for_pydantic_v2, create_robust_openapi_function
9798

9899
logger = logging.getLogger("google_adk." + __name__)
99100

@@ -686,6 +687,13 @@ async def internal_lifespan(app: FastAPI):
686687
tracer_provider = trace.get_tracer_provider()
687688
register_processors(tracer_provider)
688689

690+
# Apply Pydantic v2 compatibility patches before creating FastAPI app
691+
patches_applied = patch_types_for_pydantic_v2()
692+
if patches_applied:
693+
logger.info("Pydantic v2 compatibility patches applied successfully")
694+
else:
695+
logger.warning("Pydantic v2 compatibility patches could not be applied")
696+
689697
# Run the FastAPI server.
690698
app = FastAPI(lifespan=internal_lifespan)
691699

@@ -698,6 +706,10 @@ async def internal_lifespan(app: FastAPI):
698706
allow_headers=["*"],
699707
)
700708

709+
# Replace default OpenAPI function with robust version
710+
app.openapi = create_robust_openapi_function(app)
711+
logger.info("Robust OpenAPI generation enabled with Pydantic v2 error handling")
712+
701713
@app.get("/list-apps")
702714
async def list_apps() -> list[str]:
703715
return self.agent_loader.list_agents()

0 commit comments

Comments
 (0)