- Is it doable to ask the assistant to export conversation messages to JSONL or CSV?
Yes, it’s doable, but there’s a catch—tokens. A "token" isn’t the same as a word or character. It’s like chunks of text, and even simple words can split into multiple tokens depending on the language or formatting. Models like GPT-4 have a max token limit per request (e.g., 8,192 or 32,768 tokens), which includes everything—the system setup, your messages, and the assistant's responses.
So, exporting all 164 messages at once might hit that limit. To handle this:
- Break it down: Ask for smaller chunks (like 50 messages at a time). Then, combine them into a JSONL or CSV later using a script or tool.
import json import requests # Azure OpenAI API details AZURE_OPENAI_ENDPOINT = "https://your-endpoint.openai.azure.com/" API_VERSION = "2024-05-01-preview" API_KEY = "your-api-key" THREAD_ID = "thread_id_here" # Replace with the specific thread ID # Configuration OUTPUT_FILE = "conversation.jsonl" # Output file name LAST_N_MESSAGES = 50 # Number of last messages to export def fetch_messages(thread_id, last_n): """ Fetch the last N messages from a thread using Azure OpenAI API. """ url = f"{AZURE_OPENAI_ENDPOINT}openai/threads/{thread_id}/messages" headers = { "api-key": API_KEY, "Content-Type": "application/json" } params = { "api-version": API_VERSION } response = requests.get(url, headers=headers, params=params) if response.status_code != 200: print(f"Error fetching messages: {response.status_code} - {response.text}") return None messages = response.json() # Extract the last N messages return messages["messages"][-last_n:] def export_to_jsonl(messages, output_file): """ Save messages to a JSONL file . """ with open(output_file, "w") as f: for message in messages: json.dump(message, f) f.write("\n") print(f"Exported {len(messages)} messages to {output_file}") def main(): # Fetch the last N messages messages = fetch_messages(THREAD_ID, LAST_N_MESSAGES) if not messages: print("No messages to export.") return # Export messages to JSONL export_to_jsonl(messages, OUTPUT_FILE) if __name__ == "__main__": main()
- Use a simpler format: If you just grab the raw data and format it yourself (in your script), you’ll save a lot of tokens, specially in the JSONL format.
- Is there anywhere to get a closer look on error details?
The API gives you lots of clues when something goes wrong:
-
incomplete_details.reason
will tell you why the task failed—like in your case,max_prompt_tokens
. -
usage.prompt_tokens
shows how many tokens your input used. -
total_tokens
is the total used (input + output). If it’s above the limit, the request won’t work.
These, you can check to adjust your request to fit within the limits. Tokens might sound technical, but think of it this way: it is just how the system counts chunks of text, not letters or even words or characters. Feel free to ask for further breakdowns if you need help!🫡