0
0
FastAPIframework~20 mins

Streaming responses in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Streaming Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this FastAPI streaming response output?
Consider this FastAPI endpoint that streams numbers as text lines. What will the client receive when calling this endpoint?
FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio

app = FastAPI()

async def number_stream():
    for i in range(3):
        yield f"Number: {i}\n"
        await asyncio.sleep(0.1)

@app.get("/numbers")
async def stream_numbers():
    return StreamingResponse(number_stream(), media_type="text/plain")
AThe client receives a JSON array ["Number: 0", "Number: 1", "Number: 2"] all at once.
BThe client receives a streamed response with lines: 'Number: 0', 'Number: 1', 'Number: 2' each followed by a newline, arriving with small delays.
CThe client receives a single string 'Number: 0Number: 1Number: 2' without newlines or delays.
DThe client receives an error because StreamingResponse cannot stream async generators.
Attempts:
2 left
💡 Hint
Think about what StreamingResponse does with an async generator and the media type.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a streaming response from a synchronous generator?
You want to stream data from a synchronous generator function in FastAPI. Which code snippet correctly wraps it for StreamingResponse?
FastAPI
def sync_generator():
    for i in range(3):
        yield f"data {i}\n"
Areturn StreamingResponse(sync_generator(), media_type="text/plain")
Breturn StreamingResponse(iter(sync_generator()), media_type="text/plain")
Creturn StreamingResponse(async_generator(sync_generator()), media_type="text/plain")
Dreturn StreamingResponse(sync_generator, media_type="text/plain")
Attempts:
2 left
💡 Hint
StreamingResponse accepts any iterable or async iterable as first argument.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI streaming endpoint raise a RuntimeError?
Examine this code snippet. Why does it raise 'RuntimeError: async generator ignored' when called?
FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

def sync_gen():
    yield "hello\n"

@app.get("/stream")
async def stream():
    return StreamingResponse(sync_gen(), media_type="text/plain")
ABecause the sync generator yields strings instead of bytes.
BBecause StreamingResponse expects an async generator but got a sync generator in an async endpoint.
CBecause the endpoint is async but returns a sync generator without wrapping it in an async iterator.
DBecause StreamingResponse requires the media_type to be 'application/octet-stream' for generators.
Attempts:
2 left
💡 Hint
Think about how async endpoints handle sync generators in StreamingResponse.
state_output
advanced
2:00remaining
What is the output of this FastAPI streaming response with stateful generator?
Given this code, what will the client receive when calling /count?
FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

class Counter:
    def __init__(self):
        self.count = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.count >= 3:
            raise StopIteration
        self.count += 1
        return f"Count: {self.count}\n"

@app.get("/count")
def count_stream():
    counter = Counter()
    return StreamingResponse(counter, media_type="text/plain")
AThe client receives streamed lines: 'Count: 1', 'Count: 2', 'Count: 3' each followed by newline.
BThe client receives a JSON array ["Count: 1", "Count: 2", "Count: 3"] all at once.
CThe client receives a single string 'Count: 123' without newlines or streaming.
DThe client receives an error because Counter is not an async iterable.
Attempts:
2 left
💡 Hint
StreamingResponse can stream from any iterable, including stateful ones.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI StreamingResponse and async generators is TRUE?
Select the correct statement about using StreamingResponse with async generators in FastAPI.
AStreamingResponse can only stream synchronous generators; async generators cause runtime errors.
BStreamingResponse automatically converts synchronous generators to async generators internally.
CStreamingResponse requires the async generator to be fully consumed before sending any data to the client.
DStreamingResponse can stream async generators, but the endpoint must be async and return the StreamingResponse directly without awaiting the generator.
Attempts:
2 left
💡 Hint
Consider how async generators and StreamingResponse interact in async endpoints.