Strange citation formats in OpenAI Assistant responses

I’m working with the OpenAI Assistant API and encountering odd citation formats in the outputs. When I stream data, citations appear as 【4:2†source】 or as citeturnXfileY, which aren’t proper references.

client.beta.threads.messages.create(
    thread_id=my_thread,
    role="user",
    content=user_query
)

stream_run = client.beta.threads.runs.create(
    thread_id=my_thread,
    assistant_id=my_assistant,
    stream=True,
    tool_choice={"type": "file_search"},
)

response_text = ""
for chunk in stream_run:
    if chunk.event == "thread.message.delta":
        content_delta = chunk.data.delta.content
        if content_delta and content_delta[0].type == "text":
            chunk_text = content_delta[0].text.value
            response_text += chunk_text
            yield {"content": chunk_text}
    if chunk.event == "thread.run.completed":
        break

How can I achieve standard citation formatting instead of these unusual symbols?

This happens because of how OpenAI handles file search results during streaming vs. complete responses. Those markers are internal references that only get resolved after the full response finishes generating. I’ve had good luck with a simple post-processing fix - once your streaming’s done, just make one more call to grab the finalized message content. The performance hit is pretty minimal since you’re only fetching that last message, and your citations will display properly. Works consistently across different assistant setups I’ve tested.

yeah, i totally get that frustration! streaming kinda messes up citations. after you finish your loop, just run client.beta.threads.messages.list(thread_id=my_thread, limit=1) to get the correct format. it’s a lil annoying, but it should fix it.

this bug drove me nuts for weeks! the streaming api dumps raw citation tokens before processing them. quick fix - i regex replace those weird patterns with temp placeholders during streaming, then swap them back when fetching the final message. not pretty but works fine.

The Problem:

You’re seeing unusual citation formats like 【4:2†source】 or citeturnXfileY in the streamed output of the OpenAI Assistant API’s file search functionality. These aren’t standard citations, making your output less readable and usable. This occurs because the streaming API delivers raw text chunks before the full response is processed. The final, correctly formatted citations only appear once the entire response is generated.

:gear: Step-by-Step Guide:

  1. Stream the Response (as you are currently doing): Your existing code for streaming the response from the OpenAI Assistant API is a good starting point. Continue to use the streaming approach for a better user experience while the response is being generated. This will show a continuous stream of text.

  2. Fetch the Final, Formatted Message: After your streaming loop completes, make another API call to retrieve the final, fully processed message. This call will contain the correctly formatted citations. You can do this using the client.beta.threads.messages.list() method:

import openai

# ... your existing code ...

for chunk in stream_run:
    # ... your existing code ...

# Get the final, fully formatted message
final_message = openai.beta.threads.messages.list(thread_id=my_thread, limit=1)

# Access the content of the final message.  The exact structure depends on the API response
#  but it should contain the correctly formatted citations.
response_text = final_message.data[0].content[0].text.value # Adjust based on actual API response structure

print(response_text)  #This will now contain the properly formatted citations

:mag: Common Pitfalls & What to Check Next:

  • API Response Structure: The structure of the final_message object might vary slightly depending on the OpenAI API version. Carefully inspect the response from client.beta.threads.messages.list() to locate the correctly formatted citation text. Adjust the code above to extract the text accordingly. If you are still encountering issues, print the complete final_message object to examine its structure.

  • Error Handling: Add error handling to gracefully manage potential issues like network errors or API rate limits when fetching the final message.

  • Rate Limits: Be mindful of OpenAI’s API rate limits. If you’re making many requests, consider adding delays between requests to avoid exceeding the limits.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

I’ve encountered this exact issue before. The unusual citation formats arise because the streaming responses and standard responses handle citations differently. When you stream, you receive raw text chunks without the necessary post-processing that converts internal citation markers into standard references. Essentially, the API creates these placeholders during streaming, but it formats them correctly once the entire response is complete. To resolve this, after your streaming loop concludes, you should retrieve the final message using client.beta.threads.messages.list(). This will provide you with properly formatted citations, rectifying the discrepancy caused by the streaming process.

Yeah, that streaming behavior is expected with the current API - it’s annoying but that’s how it works. Those weird 【4:2†source】 formats are just temporary placeholders that don’t get resolved until the full response is done. I’ve learned to treat streaming as just a preview for UX, then grab the final message content afterward to get the real citations. The server does post-processing to convert those internal markers into proper references, but only after the assistant finishes everything. Not ideal for development, but it’s a current limitation of how streaming handles file search citations.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.