Repository navigation
Sentry not logging traces for FastAPI's StreamingResponse endpoint #5002
Description
Activity
Assigning to @getsentry/support for routing ⏲️
- moved this from Waiting for: Support to Waiting for: Product Owner in GitHub Issues with 👀 3
on Oct 23, 2025 alexander-alderman-webb commented
on Oct 24, 2025 ContributorMore actionsHi @Macok,
Thank for your report. The last comment you made is really useful. Based on the remark, it sounds like you are able to get transactions for endpoints returning
StreamingResponse, but not together with yourdo_generate_summary()function.With above code, something even more strange happens. Now, I can see transactions created by sentry_sdk for my /generate_summary endpoint, but my_transaction transaction and my_span span are missing!
You could be reaching size limits on the ingestion. To confirm can you add
debug=Truewhen initializing Sentry, like sosentry_sdk.init( dsn=os.environ['SENTRY_DSN'], environment=os.getenv("SENTRY_ENV", "production"), send_default_pii=True, traces_sample_rate=1.0, + debug=True, )Then you should see a message like the following if the serialized transaction is too large:
ERROR: Unexpected status code: 400 (body: b'{"detail":"envelope exceeded size limits for type \'event\' (https://develop.sentry.dev/sdk/envelopes/#size-limits)"}')
Reacted by mpocwierz- moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3
on Oct 24, 2025 That was it! Thanks for quick help!
Inside
do_generate_summary, I'm streaming response from OpenAI chunk by chunk, and streaming the chunks to my frontend:response = openai.chat.completions.create( model=model, messages=messages, stream=True ) for chunk in response: ... yield f'event: CHUNK\ndata: {chunk.choices[0].delta.content}'Apparently, this creates hundreds of spans within Sentry transaction, leading to
envelope exceeded size limitserror.Can you suggest some solution?
Maybe there's a way to have my chunk processing logic grouped as a single span which doesn't create any child spans?
Something like:with sentry_sdk.create_span(name='chunk_processing', allow_child_spans=False): # chunk processing logic showed above goes here4 remaining items
Thanks for the answer! But I'm already on
2.42.1.
I was able to fixenvelope exceeded size limitsby removing theyieldline from my code.response = openai.chat.completions.create( model=model, messages=messages, stream=True ) for chunk in response: ... yield f'event: CHUNK\ndata: {chunk.choices[0].delta.content}' # <-- WORKS FINE WITHOUT THIS LINEIt seems like
sentry_sdkcreates a span for each chunk of data streamed from my backend to the frontend.Is this something the Sentry team would consider fixing? Streaming chunks to the frontend is rather common for LLM-powered apps.
For now, do you see any walkaround for me?@alexander-alderman-webb
Did you manage to take a look at this?alexander-alderman-webb commented
on Oct 29, 2025 ContributorMore actionsHi @Macok,
We are aware of the problem and there is an initiative on the way that solves the size restriction in the long-term.
In terms of a work-around, you must cut down the size of the transaction holding the spans generated by our integrations for FastAPI and OpenAI.
What you choose to omit depends on which telemetry you care about most. Storing the LLM's response text likely accounts for most of the transaction's size, so I would write a
before_send_transaction()callback that truncates the response text.You can experiment more with the
before_send_transaction()callback. Only the return value of the function will be emitted fromsentry_sdk.import sentry_sdk from sentry_sdk.consts import SPANDATA, OP def before_send_transaction(transaction, hint): for span in transaction["spans"]: if span["op"] == OP.GEN_AI_CHAT: span["data"][SPANDATA.GEN_AI_RESPONSE_TEXT] = "Redacted" return transaction sentry_sdk.init( dsn=os.environ['SENTRY_DSN'], environment=os.getenv("SENTRY_ENV", "production"), send_default_pii=True, traces_sample_rate=1.0, + debug=True, + before_send_transaction=before_send_transaction, )Reacted by Tibin Lukose- moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3
on Oct 29, 2025 Ok, I'll experiment with
before_send_transaction👍
Thanks for help!Work is underway to solve this out of the box, in the meantime cutting down on the event size in
before_send_transactionis the way to go -- I'll close.@alexander-alderman-webb Thank you, do we have a possible fix on sentry.io anytime soon?
Metadata
Metadata
Assignees
Projects
- StatusShow more project fieldsWaiting for: Product Owner
Environment
SaaS (https://sentry.io/)
Steps to Reproduce
I initialised
sentry_sdkwithIn the
Trace Explorer, I can see traces from all endpoints of my app, but not below one:I tried to manually add transaction inside
do_generate_summary()generator like below:With above code, something even more strange happens. Now, I can see transactions created by
sentry_sdkfor my/generate_summaryendpoint, butmy_transactiontransaction andmy_spanspan are missing!Expected Result
Traces for FastAPI endpoints returning a
StreamingResponseshould be sent to Sentry.Actual Result
Traces for FastAPI endpoints returning a
StreamingResponseare not being sent to Sentry.Version
sentry-sdk==2.42.1fastapi==0.119.1Python 3.12.12