Enable request body OpenAPI spec for OpenAI endpoints #865
+3
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What & Why
FastAPI by default supports OpenAPI spec https://fastapi.tiangolo.com/tutorial/body/#automatic-docs which will be used to generate docs (e.g., Swagger UI). The way to enable request body documentation for post based endpoint is to include the custom data model into the function signature.
The way we use now
request = ChatCompletionRequest(**await raw_request.json())
, per https://fastapi.tiangolo.com/tutorial/body/#results , is essentially the same as defining asrequest: ChatCompletionRequest
in the function signature. And the raw request usage won't be impacted per https://fastapi.tiangolo.com/advanced/using-request-directly/The benefit of this change is to enable an interactive Swagger UI "Try it out" experience.
Without this change, the Swagger UI (the
/docs
endpoint of the server) shows no request body for the openai endpoint thus can'tTry it out
(Thus the Swagger UI is basically unusable)With this change, the Swagger UI will show the request body and we can
Try it out
through the UI directly to hit the server.This may help with many live demo use cases through the Swagger UI.