Skip to content

fix(py): HttpErrorWireFormat JSON serialization in FastAPI/Flask#4954

Merged
huangjeff5 merged 11 commits intomainfrom
jh-fastapi-fix-2
Mar 20, 2026
Merged

fix(py): HttpErrorWireFormat JSON serialization in FastAPI/Flask#4954
huangjeff5 merged 11 commits intomainfrom
jh-fastapi-fix-2

Conversation

@huangjeff5
Copy link
Copy Markdown
Contributor

@huangjeff5 huangjeff5 commented Mar 19, 2026

When validation errors or flow exceptions occur, FastAPI and Flask handlers called json.dumps(get_callable_json(ex)) directly. get_callable_json() returns HttpErrorWireFormat (Pydantic BaseModel), which json.dumps cannot serialize — TypeError was raised and clients received 500 instead of 422/400.

Fix is to make sure get_callable_json returns a dict that can be json.dump() immediately.

Also enabled the case where you want to expose a flow directly as an HTTP function.

@app.post('/chat', response_model=None)
@genkit_fastapi_handler(ai)
@ai.flow()
async def chat(prompt: str) -> str:
    response = await ai.generate(prompt=prompt)
    return response.text

Previously you had to write:

@app.post('/chat', response_model=None)
@genkit_fastapi_handler(ai)
async def chat():
    return my_flow

@ai.flow()
async def chat(prompt: str) -> str:
    response = await ai.generate(prompt=prompt)
    return response.text

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 resolves critical JSON serialization issues in FastAPI and Flask error handling, ensuring that API clients receive properly formatted error responses instead of internal server errors. It also enhances the robustness of stream processing by correctly propagating cancellation and exceptions, preventing unexpected crashes. These changes improve the reliability and user experience of applications built with Genkit's Python framework.

Highlights

  • FastAPI/Flask Error Serialization Fix: Addressed an issue where HttpErrorWireFormat (a Pydantic BaseModel) was not correctly serialized to JSON in FastAPI and Flask error handlers, leading to TypeError and 500 responses instead of expected 4xx/5xx error codes with valid JSON.
  • Stream Cancellation Robustness: Improved the send_chunk function in _action.py to gracefully handle stream cancellations and exceptions, ensuring they are propagated to the result_future without crashing the callback.
  • Comprehensive Testing: Introduced new unit tests for FastAPI error handling to validate that both 400 (missing data) and 500 (flow exception) responses return valid JSON, and added a test for stream cancellation behavior.
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.

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
Copy Markdown
Contributor

@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 addresses a TypeError that occurred when serializing HttpErrorWireFormat Pydantic models to JSON in FastAPI and Flask error handlers. The fix correctly converts the Pydantic model to a dictionary before JSON serialization. The PR also includes a valuable improvement to streaming actions by properly handling cancellations and exceptions in callbacks, preventing potential crashes. New tests are added for both fixes. My review includes a couple of suggestions to make the new test assertions more specific and robust.

Comment thread py/plugins/fastapi/tests/fastapi_test.py Outdated
Comment thread py/plugins/fastapi/tests/fastapi_test.py Outdated
Comment thread py/plugins/fastapi/src/genkit/plugins/fastapi/handler.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Comment thread py/plugins/fastapi/tests/fastapi_test.py Outdated
Comment thread py/plugins/fastapi/src/genkit/plugins/fastapi/handler.py Outdated
Comment thread py/packages/genkit/tests/genkit/core/error_test.py Outdated
@huangjeff5
Copy link
Copy Markdown
Contributor Author

/gemini review

@huangjeff5 huangjeff5 marked this pull request as ready for review March 19, 2026 17:23
Copy link
Copy Markdown
Contributor

@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 correctly addresses a JSON serialization issue with HttpErrorWireFormat in FastAPI and Flask handlers. The core fix, which involves changing get_callable_json to return a dictionary, is sound and effectively prevents the TypeError. The changes are well-supported by updated unit tests and new integration tests for FastAPI that confirm the fix. I've included a couple of suggestions to enhance code consistency and test maintainability.

Comment thread py/plugins/fastapi/tests/fastapi_test.py Outdated
Comment thread py/plugins/flask/src/genkit/plugins/flask/handler.py Outdated
Comment thread py/plugins/fastapi/src/genkit/plugins/fastapi/handler.py Outdated
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Mar 19, 2026
@huangjeff5 huangjeff5 requested a review from pavelgj March 19, 2026 23:53
@huangjeff5 huangjeff5 merged commit 6b9e511 into main Mar 20, 2026
17 checks passed
@huangjeff5 huangjeff5 deleted the jh-fastapi-fix-2 branch March 20, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation fix python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants