Skip to content

Add typed request/response models and streaming toggle to the API - #2

Open
stanleyu911 wants to merge 1 commit into
qwshen:mainfrom
stanleyu911:feat/api-swagger-improvements
Open

Add typed request/response models and streaming toggle to the API#2
stanleyu911 wants to merge 1 commit into
qwshen:mainfrom
stanleyu911:feat/api-swagger-improvements

Conversation

@stanleyu911

Copy link
Copy Markdown

Summary

  • /completion, /search, /history parsed the request body manually and read sid/ctx-api-token straight from query params/headers, so the OpenAPI schema (and Swagger's "Try it out") showed empty request bodies and no documented parameters.
  • Added Pydantic request models and declared sid/ctx-api-token as Query/Header params, now fully documented in /openapi.json. ctx-api-token stays optional at the framework level so a missing token still returns 401 rather than a generic 422.
  • Added a stream query param (default true, existing behavior unchanged) to all three endpoints; stream=false returns one consolidated JSON response instead of SSE - useful for Swagger and simple non-streaming clients.
  • Added docs/api-usage-examples.md with reusable curl examples.
  • Added pydantic to requirements.txt explicitly (already an implicit dependency of fastapi, now used directly).

Test plan

  • Verified /openapi.json and Swagger UI render correctly for all three endpoints in both modes.
  • Verified 401/422 behavior unchanged.
  • Verified end-to-end against a local pgvector + OpenAI-compatible LLM setup in both streaming and stream=false modes.

The completion/search/history endpoints took a raw Request and parsed
the body manually, and read sid/ctx-api-token straight from query
params/headers without declaring them - so the auto-generated
OpenAPI schema (and Swagger UI's "Try it out") showed empty {}
request bodies and no documented parameters.

- Replace manual request.json() parsing with Pydantic request models
  (CompletionRequest, SearchRequest).
- Declare sid/ctx-api-token as FastAPI Query/Header params so they
  show up in the OpenAPI schema. ctx-api-token stays optional at the
  framework level so a missing token still returns the existing 401,
  rather than FastAPI's own 422 validation error pre-empting it.
- Add a stream query param (default true, preserving existing
  behavior) to all three endpoints. stream=false buffers the same
  underlying generator server-side and returns one consolidated JSON
  response (CompletionResponse/SearchResponse/HistoryResponse) with a
  declared schema - useful for Swagger's "Try it out" and simple
  non-streaming clients, since Swagger UI has no special handling for
  text/event-stream and otherwise just dumps the raw SSE lines as text.
- Add docs/api-usage-examples.md with reusable curl examples for all
  three endpoints, streaming and non-streaming.
- Add pydantic to requirements.txt explicitly, since our code now
  imports it directly rather than relying on it only as fastapi's
  transitive dependency.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uwq94g5qTJmrQyRnib67P
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.

1 participant