Skip to content

StreamingResponse and pydantic.BaseModel? #2363

Closed Answered by jamesbraza
jamesbraza asked this question in Q&A
Discussion options

You must be logged in to vote

Note that #419 is related.

You can do something like this (pydantic==2.5.2, starlette==0.27.0):

from collections.abc import AsyncIterable, Iterable

from pydantic import BaseModel
from starlette.background import BackgroundTask
from starlette.concurrency import iterate_in_threadpool
from starlette.responses import JSONResponse, StreamingResponse


class JSONStreamingResponse(StreamingResponse, JSONResponse):
    """StreamingResponse that also render with JSON."""

    def __init__(
        self,
        content: Iterable | AsyncIterable,
        status_code: int = 200,
        headers: dict[str, str] | None = None,
        media_type: str | None = None,
        background: BackgroundTask | 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jamesbraza
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant