Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request serialization for fastapi /docs #8530

Merged
merged 4 commits into from
Jun 11, 2024

Conversation

zhzLuke96
Copy link
Contributor

This commit addresses a serialization issue in the PredictBody model that has been present since version 4.0. The request attribute, being of type Request, could not be serialized automatically, affecting FastAPI's auto-generated documentation (/openapi.json).
By adding a custom __get_pydantic_json_schema__ method, the serialization schema is manually defined, ensuring correct documentation generation.
This is also one of the reasons why applications like "stable-diffusion-webui," which require /docs pages, have not updated to newer versions of Gradio.

Closes: #7287

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jun 11, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/c841ac8484eb4d18ef185ee583c75f4a6ff73b1e/gradio-4.36.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@c841ac8484eb4d18ef185ee583c75f4a6ff73b1e#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/c841ac8484eb4d18ef185ee583c75f4a6ff73b1e/gradio-client-1.1.1.tgz

@@ -110,6 +110,24 @@ class PredictBody(BaseModel):
None # dictionary of request headers, query parameters, url, etc. (used to to pass in request for queuing)
)

def __get_pydantic_json_schema__(self, source_type: Any, handler):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Based on the docs, this should be a class method!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, @freddyaboulton, for pointing that out! I've updated the method to be a class method as per the documentation. Please review the latest commit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks - added a unit test

@gradio-pr-bot
Copy link
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Fix request serialization for fastapi /docs

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton freddyaboulton enabled auto-merge (squash) June 11, 2024 19:12
@freddyaboulton freddyaboulton merged commit d429690 into gradio-app:main Jun 11, 2024
7 checks passed
@pngwn pngwn mentioned this pull request Jun 11, 2024
@pngwn pngwn mentioned this pull request Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI docs not working
3 participants