@@ -393,3 +393,76 @@ def test_code_interpreter_with_conversation(self, **kwargs):
393393 project_client .agents .delete_version (agent_name = agent .name , agent_version = agent .version )
394394 openai_client .conversations .delete (conversation_id = conversation .id )
395395 print ("Cleanup completed" )
396+
397+ @servicePreparer ()
398+ @pytest .mark .skipif (
399+ condition = (not is_live_and_not_recording ()),
400+ reason = "Skipped because we cannot record network calls with OpenAI client" ,
401+ )
402+ def test_code_interpreter_with_file_in_conversation (self , ** kwargs ):
403+ """
404+ Test using CodeInterpreterTool with file upload within a conversation.
405+
406+ This test reproduces the 500 error seen in the sample when using
407+ code interpreter with uploaded files in conversations.
408+
409+ This tests:
410+ - Uploading a real file (not BytesIO) for code interpreter
411+ - Using code interpreter with files in conversation
412+ - Server-side code execution with file access and chart generation
413+ """
414+
415+ model = self .test_agents_params ["model_deployment_name" ]
416+ import os
417+
418+ with (
419+ self .create_client (operation_group = "agents" , ** kwargs ) as project_client ,
420+ project_client .get_openai_client () as openai_client ,
421+ ):
422+ # Use the same CSV file as the sample
423+ asset_file_path = os .path .abspath (
424+ os .path .join (
425+ os .path .dirname (__file__ ),
426+ "../../../samples/agents/assets/synthetic_500_quarterly_results.csv" ,
427+ )
428+ )
429+
430+ # Upload file using open() with rb mode, just like the sample
431+ with open (asset_file_path , "rb" ) as f :
432+ uploaded_file = openai_client .files .create (file = f , purpose = "assistants" )
433+ print (f"File uploaded: { uploaded_file .id } " )
434+
435+ # Create agent with code interpreter - matching sample exactly
436+ agent = project_client .agents .create_version (
437+ agent_name = "agent-code-interpreter-with-file-pbatum1" ,
438+ definition = PromptAgentDefinition (
439+ model = model ,
440+ instructions = "You are a helpful assistant." ,
441+ tools = [CodeInterpreterTool (container = CodeInterpreterToolAuto (file_ids = [uploaded_file .id ]))],
442+ ),
443+ description = "Code interpreter agent for data analysis and visualization." ,
444+ )
445+ print (f"Agent created: { agent .id } " )
446+
447+ # Create conversation
448+ conversation = openai_client .conversations .create ()
449+ print (f"Conversation created: { conversation .id } " )
450+
451+ # Use the same prompt as the sample - requesting chart generation
452+ print ("\n --- Turn 1: Create bar chart ---" )
453+ response_1 = openai_client .responses .create (
454+ conversation = conversation .id ,
455+ input = "Could you please create bar chart in TRANSPORTATION sector for the operating profit from the uploaded csv file and provide file to me?" ,
456+ extra_body = {"agent" : {"name" : agent .name , "type" : "agent_reference" }},
457+ )
458+
459+ response_1_text = response_1 .output_text
460+ print (f"Response 1: { response_1_text [:200 ]} ..." )
461+
462+ print ("\n ✓ Code interpreter with file in conversation successful!" )
463+
464+ # Cleanup
465+ project_client .agents .delete_version (agent_name = agent .name , agent_version = agent .version )
466+ openai_client .conversations .delete (conversation_id = conversation .id )
467+ openai_client .files .delete (uploaded_file .id )
468+ print ("Cleanup completed" )
0 commit comments