How-to guide
How to Handle Ad-Injection Errors in FastAPI
This guide shows how to handle ad-injection errors in a FastAPI app, the goal being to make sure the user still gets a reply if the ad service fails. It builds on the Monetzly gRPC service (no Python SDK exists for this stack), so the approach is specific to how FastAPI produces and streams responses.
Overview
Monetization should never break your FastAPI app. The Monetzly SDK is built for this: if the ad service connection fails or errors mid-stream, it falls back to yielding your original, un-injected token stream, so the user still gets a complete answer.
You should still wrap the injection loop in your normal error handling so a failure degrades to a plain FastAPI response rather than a broken request.
FastAPI apps are usually built for LLM API backends, SSE/streaming chat endpoints, and agent microservices, so handle ad-injection errors typically comes up while a user is mid-conversation, the moment where monetization has to be additive rather than disruptive.
How this works on FastAPI
There is no official Monetzly Python SDK — the package is Node-only. The supported path for a Python stack is the gRPC service the package ships as tps_alter.proto: generate Python stubs and open a ProcessStream bidi call (StartRequest, then a TokenRequest per LLM token, then StopRequest), reading back the ad-injected TokenResponses. Alternatively, run a tiny Node sidecar that uses the SDK and stream tokens to it. Either way, confirm the auth handshake for non-JS clients before publishing.
Since FastAPI is a Python stack with no official SDK, this task runs through the shipped tps_alter.proto gRPC service (or a Node sidecar); the auth handshake for a direct client is still TODO_VERIFY.
Steps
- Wrap the injection loop in your standard FastAPI error handling.
- Rely on the SDK's automatic fallback to the original stream on ad-service failure.
- Log injection failures so you can monitor fill and uptime separately from your model.
Integration snippet
# 1) Generate stubs from the proto shipped in the SDK package:
# python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. tps_alter.proto
import grpc
import tps_alter_pb2 as pb
import tps_alter_pb2_grpc as rpc
async def inject(llm_tokens, prompt, session_id, api_key):
# TODO_VERIFY: how is the API key passed for a direct gRPC client?
# (metadata key name / channel credentials). Confirm with Monetzly.
creds = grpc.ssl_channel_credentials()
async with grpc.aio.secure_channel("your-server.com:443", creds) as ch:
stub = rpc.TPSAlterServiceStub(ch)
async def requests():
yield pb.StreamRequest(start=pb.StartRequest(
prompt=prompt, session_id=session_id, metadata={"api_key": api_key}))
async for tok in llm_tokens:
yield pb.StreamRequest(token=pb.TokenRequest(token=tok))
yield pb.StreamRequest(stop=pb.StopRequest(session_id=session_id))
async for resp in stub.ProcessStream(requests()):
if resp.HasField("token"):
yield resp.token.token # ad-injected token
# In a FastAPI route, feed your LLM's token generator into inject(...) and
# return the results with StreamingResponse.Frequently asked questions
Start monetizing in about 5 minutes
Wrap your existing LLM response stream with the Monetzly SDK and earn on every session, no paywall required.