Skip to content

Conversation

@mmphego
Copy link

@mmphego mmphego commented Nov 12, 2025

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
The Swagger UI documentation endpoint (/docs) returns 500 Internal Server Error when using Pydantic v2 due to multiple compatibility issues:

  1. httpx.Client Schema Generation: PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.IsInstanceSchema (<class 'httpx.Client'>)
  2. MCP ClientSession Compatibility: Deprecated __modify_schema__ methods cause conflicts with Pydantic v2
  3. Modern Generic Types: types.GenericAlias (e.g., list[str], dict[str, int]) not handled properly
  4. Recursion Errors: Complex model schemas cause recursion depth exceeded errors during OpenAPI generation

Solution:
This PR implements a comprehensive Pydantic v2 compatibility layer that:

  • Patches problematic types with proper __get_pydantic_core_schema__ methods
  • Removes deprecated Pydantic v1 methods (__modify_schema__) that conflict with v2
  • Adds robust OpenAPI generation with recursion protection and comprehensive error handling
  • Provides fallback schemas to ensure Swagger UI always works, even with complex models
  • Maintains backward compatibility while enabling Pydantic v2 functionality

The solution is non-invasive and applies patches during application startup before FastAPI initialization.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Unit Test Results:

# Comprehensive test coverage for all compatibility functions
Added: tests/unittests/utils/test_pydantic_v2_compatibility.py
- 15+ test cases covering success/failure scenarios
- Tests for patch application, schema generation, and error handling
- Tests for robust OpenAPI function with various error conditions
- Mock-based validation of all patch functions
- Proper exception handling and logging validation

Manual End-to-End (E2E) Tests:

Test Environment:

  • OS: macOS
  • Python: 3.12
  • ADK: 1.17.0
  • Pydantic: 2.12.3

Test Steps:

  1. Start ADK API server: uv run adk api_server --port 8000
  2. Navigate to Swagger UI: http://localhost:8000/docs
  3. Verify OpenAPI JSON generation: http://localhost:8000/openapi.json

Before Fix:

INFO:     127.0.0.1:50256 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:50256 - "GET /openapi.json HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.IsInstanceSchema (<class 'httpx.Client'>)

After Fix:

INFO:     Pydantic v2 compatibility patches applied successfully  
INFO:     Robust OpenAPI generation enabled with Pydantic v2 error handling
INFO:     127.0.0.1:50256 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:50256 - "GET /openapi.json HTTP/1.1" 200 OK

Swagger UI now loads successfully with full API documentation, interactive endpoint testing, and proper schema generation.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Implementation Details:

Files Changed:

  1. src/google/adk/utils/pydantic_v2_compatibility.py - New compatibility module (390 lines)
  2. src/google/adk/cli/adk_web_server.py - Integration of patches into web server startup
  3. tests/unittests/utils/test_pydantic_v2_compatibility.py - Comprehensive unit tests (229 lines)

Key Features:

  • Automatic Patch Detection: Patches are applied only if the problematic types exist
  • Graceful Degradation: Fallback schemas ensure Swagger UI always works
  • Comprehensive Error Handling: Handles RecursionError, AttributeError, and JSON schema failures
  • Professional Logging: Clean, informative logs without emoji characters
  • Zero Breaking Changes: Existing functionality remains unchanged

Compatibility:

  • Pydantic v1: No impact, patches only apply when needed
  • Pydantic v2: Full compatibility with proper schema generation
  • MCP Integration: Handles ClientSession schema generation issues
  • httpx Clients: Proper handling of non-serializable HTTP clients
  • Modern Python: Support for list[str], dict[str, int] generic syntax

This fix enables reliable Swagger UI functionality for all Google ADK users, resolving the widespread documentation access issues reported across multiple GitHub issues.

ankursharmas and others added 4 commits November 12, 2025 19:53
…vgScore Metric

Co-authored-by: Ankur Sharma <[email protected]>
PiperOrigin-RevId: 831413968
This module resolves Swagger UI 500 errors when using Pydantic v2 by:

- Patching MCP ClientSession with proper __get_pydantic_core_schema__ method
- Removing deprecated __modify_schema__ methods causing conflicts
- Adding compatibility for types.GenericAlias (list[str], dict[str, int])
- Patching httpx.Client and httpx.AsyncClient for schema generation
- Providing robust OpenAPI generation with recursion protection
- Comprehensive fallback schemas for error recovery

The robust_openapi_function handles RecursionError, AttributeError,
and other Pydantic v2 schema generation issues while maintaining
full OpenAPI specification compliance.

Fixes Swagger UI functionality for Google ADK with Pydantic v2.
- 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.
- Add test_pydantic_v2_compatibility.py with full test coverage
- Test successful schema generation and fallback scenarios
- Test patch application success and failure cases
- Test robust OpenAPI function with various error conditions:
  * RecursionError handling with recursion limit management
  * Generic exception handling with fallback schema
  * AttributeError handling for missing openapi method
  * Successful schema preservation
- Test logging behavior for different error types
- Test httpx Client patching functionality
- Follow ADK testing patterns with proper Google license header
- Use pytest parametrization and mocking best practices
- Clean logging without emoji characters for professional output

Ensures reliability and maintainability of Pydantic v2 compatibility
fixes with comprehensive error handling validation.
@gemini-code-assist
Copy link

Summary of Changes

Hello @mmphego, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical Pydantic v2 compatibility issues that were causing internal server errors and preventing the proper display of OpenAPI/Swagger UI documentation in the ADK API server. It introduces a new utility module that patches problematic types and provides a resilient OpenAPI schema generation mechanism. Additionally, it enhances the TrajectoryEvaluator with new criteria for evaluating tool call sequences, offering more granular control over how tool trajectories are matched.

Highlights

  • Pydantic v2 Compatibility: Introduces a comprehensive compatibility layer to ensure OpenAPI/Swagger UI endpoints function correctly with Pydantic v2, resolving issues like httpx.Client schema generation, MCP ClientSession conflicts, and handling modern generic types.
  • Robust OpenAPI Generation: Implements a robust OpenAPI generation function with recursion protection, comprehensive error handling, and fallback schemas to guarantee Swagger UI always works, even with complex or problematic models.
  • Non-Invasive Patching: The solution is non-invasive, applying necessary type patches and OpenAPI function replacement during application startup before FastAPI initialization, maintaining backward compatibility.
  • Enhanced Tool Trajectory Evaluation: Adds a new ToolTrajectoryCriterion with EXACT, IN_ORDER, and ANY_ORDER match types, allowing for more flexible and precise evaluation of agent tool call sequences.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a compatibility layer for Pydantic v2 to resolve issues with OpenAPI/Swagger UI generation. The core of the fix is in the new pydantic_v2_compatibility.py module, which patches incompatible types and provides a robust fallback mechanism for OpenAPI schema generation. This is a solid approach to ensure the service remains functional even when schema generation fails.

My review has identified several critical issues in the new test suite (test_pydantic_v2_compatibility.py) that need to be addressed. The tests are currently broken due to incorrect imports, flawed mocking, and assertions that don't match the implementation. I've also found a bug in the patching logic for types.GenericAlias. Please see my detailed comments.

Additionally, this PR includes a significant feature addition for ToolTrajectoryCriterion which seems out of scope for a bug fix. While the feature itself is well-implemented, it's generally better to separate new features from bug fixes into different pull requests to simplify review and maintain a clean commit history.

- Fix critical bug in GenericAlias handler calling (handler is callable, not object with generate_schema method)
- Completely rewrite test_pydantic_v2_compatibility.py to address all PR feedback:
  - Remove invalid imports of nested functions (__get_pydantic_core_schema__)
  - Fix assertion mismatches (OpenAPI version 3.1.0, correct API title)
  - Restructure tests to validate patching mechanism itself
  - Fix handler mocking to treat handlers as callables
  - Add comprehensive behavior tests with proper classmethod calling
- All 17 tests now pass with full coverage of patching scenarios
- Address types.GenericAlias immutability issues in tests
- Improve error handling and fallback validation
@mmphego mmphego force-pushed the fix/pydantic-v2-swagger-ui branch from 8730501 to 890129f Compare November 12, 2025 19:58
@adk-bot adk-bot added the web [Component] This issue will be transferred to adk-web label Nov 12, 2025
@rngtng
Copy link

rngtng commented Nov 13, 2025

Thanks a lot for the fix. Just tested on my machine - works again!

@mmphego
Copy link
Author

mmphego commented Nov 13, 2025

Thanks a lot for the fix. Just tested on my machine - works again!

Hopefully, we will not need a patch in the next release 😄

@ryanaiagent ryanaiagent self-assigned this Nov 13, 2025
@sedhha
Copy link

sedhha commented Nov 14, 2025

Can this be merged sooner?

@ryanaiagent
Copy link
Collaborator

Hi @mmphego , Thank you for your contribution!
Can you fix the failing unit tests and lint errors.

@ryanaiagent ryanaiagent added the awaiting-user-response Waiting for the issue author to provide necessary information (stale bot workflow) label Nov 14, 2025
@mmphego
Copy link
Author

mmphego commented Nov 14, 2025

Hi @mmphego , Thank you for your contribution! Can you fix the failing unit tests and lint errors.

Hi @ryanaiagent, addressed py3.9 compartibility, isort and formatting issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-user-response Waiting for the issue author to provide necessary information (stale bot workflow) web [Component] This issue will be transferred to adk-web

Projects

None yet

6 participants