Challenge - 5 Problems
Streaming Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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")
Attempts:
2 left
💡 Hint
Think about what StreamingResponse does with an async generator and the media type.
✗ Incorrect
StreamingResponse sends data chunks as they are yielded. Here, each 'Number: i\n' is sent with a small delay, so the client receives lines one by one.
📝 Syntax
intermediate2: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"
Attempts:
2 left
💡 Hint
StreamingResponse accepts any iterable or async iterable as first argument.
✗ Incorrect
StreamingResponse can take a synchronous generator directly. Wrapping with iter() is redundant but valid; however, option A uses iter() on a generator function call which is already an iterator, so it's redundant but works. Option A calls undefined async_generator. Option A passes the function itself, not the generator object.
🔧 Debug
advanced2: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")
Attempts:
2 left
💡 Hint
Think about how async endpoints handle sync generators in StreamingResponse.
✗ Incorrect
An async endpoint returning StreamingResponse with a sync generator causes FastAPI to try to iterate asynchronously, which fails. Wrapping the sync generator in an async iterator or making the endpoint sync fixes this.
❓ state_output
advanced2: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")
Attempts:
2 left
💡 Hint
StreamingResponse can stream from any iterable, including stateful ones.
✗ Incorrect
Counter is a synchronous iterator yielding strings with newlines. StreamingResponse streams these strings one by one to the client.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI StreamingResponse and async generators is TRUE?
Select the correct statement about using StreamingResponse with async generators in FastAPI.
Attempts:
2 left
💡 Hint
Consider how async generators and StreamingResponse interact in async endpoints.
✗ Incorrect
StreamingResponse supports async generators and streams data as it is yielded. The endpoint must be async and return StreamingResponse without awaiting the generator itself.