I’m using the Responses API with GPT-4.1 and Code Interpreter to generate downloadable .docx
files.
I’m seeing inconsistent response shapes for the same type of request. Sometimes the file is
returned with a container_file_citation, and sometimes Code Interpreter creates the file but
the response only includes the /mnt/data/... path in logs, with no file_id.
User prompt:
create a word document with a general description of the causes of the current conflict in
Iran. keep it under 5 pages
Request body sent to openai.responses.create(...):
{
"model": "gpt-4.1",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "create a word document with a general description of the causes of the current conflict in Iran. keep it under 5 pages"
}
]
}
],
"stream": false,
"include": [
"code_interpreter_call.outputs"
],
"tool_choice": {
"type": "allowed_tools",
"mode": "required",
"tools": [
{
"type": "code_interpreter"
}
]
},
"previous_response_id": "resp_...",
"instructions": "File generation contract (must follow):\n1) Invoke the code_interpreter tool to create the requested file and attach exactly one file artifact before completing your response.\n2) Use the correct extension by request type: Word -> .docx.\n3) Do not output sandbox paths, localhost links, or guessed links.\n4) A Markdown link or filename in prose does not satisfy this request; only a real attached code_interpreter file artifact does.\n5) If no attachment was created yet, continue working and do not finalize.\n6) If attachment exists, briefly confirm and include only the valid download link.\n7) If attachment fails, provide full draft content in plain text and explicitly state file generation failed.",
"tools": [
{
"type": "code_interpreter",
"container": {
"type": "auto"
}
}
]
}
When it works, the response includes a container_file_citation annotation:
{
"output": [
{
"type": "code_interpreter_call",
"status": "completed",
"container_id": "cntr_...",
"outputs": []
},
{
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"annotations": [
{
"type": "container_file_citation",
"container_id": "cntr_...",
"file_id": "cfile_...",
"filename": "Some_File.docx"
}
],
"text": "The Word document is ready.\n\n[Download the Word document](sandbox:/mnt/data/Some_File.docx)"
}
]
}
]
}
We then retrieve the file with:
await openai.containers.files.content.retrieve(fileId, {
container_id: containerId
});
When it does not work, Code Interpreter still creates the file successfully, but the response
does not include a file citation or file_id:
{
"output": [
{
"type": "code_interpreter_call",
"status": "completed",
"code": "from docx import Document\n...\noutput_filename = '/mnt/data/Causes_of_Current_Conflict_in_Iran.docx'\ndoc.save(output_filename)\noutput_filename",
"container_id": "cntr_...",
"outputs": [
{
"type": "logs",
"logs": "'/mnt/data/Causes_of_Current_Conflict_in_Iran.docx'"
}
]
},
{
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"annotations": [],
"text": "The Word document has been created.\n\n[Download the Word document](Causes_of_Current_Conflict_in_Iran.docx)"
}
]
}
]
}
In the failure case we have:
code_interpreter_call.status = "completed"container_id = "cntr_..."- file path in logs:
/mnt/data/Causes_of_Current_Conflict_in_Iran.docx - no
container_file_citation - no
file_id - assistant text contains only a filename or markdown link, not a retrievable artifact
Questions:
- Is this expected behavior for Responses API + GPT-4.1 Code Interpreter?
- Should a file saved to
/mnt/data/*.docxalways be returned as a
container_file_citation? - If not, is the recommended recovery pattern to call
containers.files.list(container_id),
match the returned file object bypath, then retrieve content using that file’sid? - Is there a prompt or tool-choice pattern that makes GPT-4.1 consistently attach/cite the
generated file instead of only logging the/mnt/data/...path? - What is the retention window for files created inside Code Interpreter containers?
- Does Code Interpreter container file retention differ from Responses API response
retention? - If using
container: { "type": "auto" }, what default container/file expiration should we
assume, and can/should we explicitly setexpires_afterfor generated artifact workflows?
Any guidance on whether the “no container_file_citation” response shape is
normal/expected would be appreciated.
@_j have you seen this?