I’m experiencing some odd citation styles when using the OpenAI Assistant API with streaming feature turned on. Here’s how I’m currently implementing it:
client.beta.threads.messages.create(
thread_id=my_thread,
role="user",
content=user_query
)
response_stream = client.beta.threads.runs.create(
thread_id=my_thread,
assistant_id=my_assistant,
stream=True,
tool_choice={"type": "file_search"},
)
full_response = ""
for stream_event in response_stream:
if stream_event.event == "thread.message.delta":
content_delta = stream_event.data.delta.content
if content_delta and content_delta[0].type == "text":
chunk = content_delta[0].text.value
full_response += chunk
yield {"data": chunk}
if stream_event.event == "thread.run.completed":
break
The issue I’m facing is that citations appear in strange formats like 【4:2†source】 and citeturnXfileY, which makes the responses appear untidy and unprofessional. Has anyone dealt with this problem? What solutions are available to reformat or address these citation styles?
The problem is streaming responses split citation data across multiple chunks, breaking the formatting tokens. When you piece them back together, you get broken citations that would’ve been fine in non-streaming mode. Don’t try fixing citations afterward - handle it during streaming. Buffer your chunks and only output complete sentences or paragraphs instead of raw fragments. This catches broken citation patterns before they hit your output. I use a simple buffer that holds chunks until it finds sentence breaks, then processes citations on the full text before sending it out. You still get that real-time feel but with properly formatted citations. Performance hit is basically nothing - just a few milliseconds delay per sentence.
I’ve encountered this same problem recently and found a reliable workaround. The streaming API indeed seems to mismanage citation formats when compared to standard responses. I implemented a post-processing solution where I use regex to identify and correct these faulty citations. For instance, I specifically target patterns such as 【\d+:\d+†.*?】 and citeturn\w+file\w+, allowing me to either eliminate them or replace them with more professional-looking alternatives. It appears that the streaming responses struggle with citation formatting that the non-streaming version handles well. If real-time output isn’t crucial for your citation-heavy tasks, opting for the non-streaming approach yields much neater results.
just run a simple string replace after you get the full response. I do full_response.replace('【', '[').replace('】', ']') and the same for those citeturn patterns. works fine and doesn’t need any fancy regex setup. streaming citations are broken by design anyway.
Been there before. Those weird citation formats are such a pain when you’re building something clean.
Skip the regex headaches and automate the whole citation cleanup. Set up a workflow that catches streaming chunks, spots broken citation patterns, and fixes them to your format in real time.
I built something like this when we needed consistent citations across different AI providers. The automation does pattern matching, format conversion, and validates citations against source docs. Takes 10 minutes to set up, saves hours of manual fixes.
You can customize the output however you want - numbered references, author-date format, or strip citations completely. The workflow adapts automatically.
Bonus: extend it for other formatting problems that come up with streaming responses. Way cleaner than hardcoding regex that breaks every time OpenAI changes their citation format.
Check out how to build this automation at https://latenode.com