1414from collections .abc import MutableSequence
1515from typing import Any , Literal , cast
1616
17- from agent_framework import AgentSession , Message
17+ from agent_framework import AgentSession , Content , Message
1818from agent_framework ._workflows ._checkpoint import InMemoryCheckpointStorage , WorkflowCheckpoint
1919from openai .types .conversations import Conversation , ConversationDeletedResource
2020from openai .types .conversations .conversation_item import ConversationItem
2828 ResponseInputImage ,
2929)
3030
31+ from ._utils import infer_media_type
32+
3133# Type alias for OpenAI Message role literals
3234MessageRole = Literal ["unknown" , "user" , "assistant" , "system" , "critic" , "discriminator" , "developer" , "tool" ]
3335
@@ -304,17 +306,23 @@ async def add_items(self, conversation_id: str, items: list[dict[str, Any]]) ->
304306 # Convert items to Messages and add to storage
305307 chat_messages : list [Message ] = []
306308 for item in items :
307- # Simple conversion - assume text content for now
308309 role = item .get ("role" , "user" )
309310 content = item .get ("content" , [])
310- first_content = cast (
311- dict [str , Any ],
312- content [0 ] if content and isinstance (content , list ) and isinstance (content [0 ], dict ) else {},
313- )
314- text_obj = first_content .get ("text" , "" )
315- text = text_obj if isinstance (text_obj , str ) else str (text_obj )
311+ contents : list [Content ] = []
312+
313+ if isinstance (content , str ):
314+ contents .append (Content .from_text (text = content ))
315+ elif isinstance (content , list ):
316+ for content_item in cast (list [object ], content ):
317+ if isinstance (content_item , dict ):
318+ agent_content = self ._to_agent_content (cast (dict [str , Any ], content_item ))
319+ if agent_content is not None :
320+ contents .append (agent_content )
316321
317- chat_msg = Message (role = role , contents = [text ])
322+ if not contents :
323+ raise ValueError ("Conversation message did not contain any supported content" )
324+
325+ chat_msg = Message (role = role , contents = contents )
318326 chat_messages .append (chat_msg )
319327
320328 # Add messages to internal storage
@@ -328,9 +336,9 @@ async def add_items(self, conversation_id: str, items: list[dict[str, Any]]) ->
328336 # Convert Message contents to OpenAI TextContent format
329337 message_content : MutableSequence [OpenAIContent ] = []
330338 for content_item in msg .contents :
331- if content_item . type == "text" :
332- # Extract text from TextContent object
333- message_content .append (TextContent ( type = "text" , text = content_item . text or "" ) )
339+ openai_content = self . _to_openai_content ( content_item )
340+ if openai_content is not None :
341+ message_content .append (openai_content )
334342
335343 # Create Message object (concrete type from ConversationItem union)
336344 message = OpenAIMessage (
@@ -390,29 +398,10 @@ async def list_items(
390398
391399 for content in msg .contents :
392400 content_type = getattr (content , "type" , None )
401+ mapped_content = self ._to_openai_content (content )
393402
394- if content_type == "text" :
395- # Text content for Message
396- text_value = getattr (content , "text" , "" )
397- message_contents .append (TextContent (type = "text" , text = text_value ))
398-
399- elif content_type == "data" :
400- # Data content (images, files, PDFs)
401- uri = getattr (content , "uri" , "" )
402- media_type = getattr (content , "media_type" , None )
403-
404- if media_type and media_type .startswith ("image/" ):
405- # Convert to ResponseInputImage
406- message_contents .append (ResponseInputImage (type = "input_image" , image_url = uri , detail = "auto" ))
407- else :
408- # Convert to ResponseInputFile
409- # Extract filename from URI if possible
410- filename = None
411- if media_type == "application/pdf" :
412- filename = "document.pdf"
413-
414- message_contents .append (ResponseInputFile (type = "input_file" , file_url = uri , filename = filename ))
415-
403+ if mapped_content is not None :
404+ message_contents .append (mapped_content )
416405 elif content_type == "function_call" :
417406 # Function call - create separate ConversationItem
418407 call_id = getattr (content , "call_id" , None )
@@ -532,6 +521,141 @@ async def list_items(
532521
533522 return paginated_items , has_more
534523
524+ @staticmethod
525+ def _to_agent_content (content : dict [str , Any ]) -> Content | None :
526+ """Convert one supported OpenAI conversation message part."""
527+ content_type = content .get ("type" )
528+ if content_type in ("text" , "input_text" , "output_text" ):
529+ text = content .get ("text" )
530+ return Content .from_text (text = text ) if isinstance (text , str ) else None
531+
532+ if content_type == "input_image" :
533+ detail = content .get ("detail" , "auto" )
534+ image_properties = {
535+ "openai_content_type" : "input_image" ,
536+ "detail" : detail if isinstance (detail , str ) else "auto" ,
537+ }
538+ image_url = content .get ("image_url" )
539+ if isinstance (image_url , str ) and image_url :
540+ return Content .from_uri (
541+ uri = image_url ,
542+ media_type = infer_media_type (uri = image_url , default = "image/png" ),
543+ additional_properties = image_properties ,
544+ )
545+ file_id = content .get ("file_id" )
546+ if isinstance (file_id , str ) and file_id :
547+ return Content .from_hosted_file (
548+ file_id = file_id ,
549+ additional_properties = image_properties ,
550+ )
551+ return None
552+
553+ if content_type == "input_file" :
554+ filename = content .get ("filename" )
555+ filename = filename if isinstance (filename , str ) else ""
556+ detail = content .get ("detail" )
557+ file_properties : dict [str , Any ] = {"openai_content_type" : "input_file" }
558+ if filename :
559+ file_properties ["filename" ] = filename
560+ if isinstance (detail , str ):
561+ file_properties ["detail" ] = detail
562+
563+ file_data = content .get ("file_data" )
564+ if isinstance (file_data , str ) and file_data :
565+ media_type = infer_media_type (
566+ filename = filename ,
567+ uri = file_data ,
568+ default = "application/octet-stream" ,
569+ )
570+ uri = file_data if file_data .startswith ("data:" ) else f"data:{ media_type } ;base64,{ file_data } "
571+ return Content .from_uri (
572+ uri = uri ,
573+ media_type = media_type ,
574+ additional_properties = file_properties ,
575+ )
576+
577+ file_url = content .get ("file_url" )
578+ if isinstance (file_url , str ) and file_url :
579+ return Content .from_uri (
580+ uri = file_url ,
581+ media_type = infer_media_type (
582+ filename = filename ,
583+ uri = file_url ,
584+ default = "application/octet-stream" ,
585+ ),
586+ additional_properties = file_properties ,
587+ )
588+
589+ file_id = content .get ("file_id" )
590+ if isinstance (file_id , str ) and file_id :
591+ return Content .from_hosted_file (
592+ file_id = file_id ,
593+ media_type = infer_media_type (filename = filename , default = "application/octet-stream" ),
594+ name = filename or None ,
595+ additional_properties = file_properties ,
596+ )
597+
598+ return None
599+
600+ @staticmethod
601+ def _to_openai_content (content : Content ) -> TextContent | ResponseInputImage | ResponseInputFile | None :
602+ """Convert one supported Agent Framework message part."""
603+ content_type = content .type
604+ if content_type == "text" :
605+ return TextContent (type = "text" , text = content .text or "" )
606+
607+ additional_properties = content .additional_properties or {}
608+ openai_content_type = additional_properties .get ("openai_content_type" )
609+ is_image = openai_content_type == "input_image" or (
610+ openai_content_type is None and bool (content .media_type and content .media_type .startswith ("image/" ))
611+ )
612+
613+ if content_type in ("data" , "uri" ):
614+ if is_image :
615+ detail = additional_properties .get ("detail" , "auto" )
616+ if detail not in ("low" , "high" , "auto" , "original" ):
617+ detail = "auto"
618+ return ResponseInputImage (
619+ type = "input_image" ,
620+ image_url = content .uri ,
621+ detail = detail ,
622+ )
623+
624+ filename = additional_properties .get ("filename" )
625+ detail = additional_properties .get ("detail" )
626+ if detail not in ("low" , "high" , "auto" ):
627+ detail = None
628+ return ResponseInputFile (
629+ type = "input_file" ,
630+ file_url = content .uri ,
631+ filename = filename if isinstance (filename , str ) else None ,
632+ detail = detail ,
633+ )
634+
635+ if content_type == "hosted_file" :
636+ if is_image :
637+ detail = additional_properties .get ("detail" , "auto" )
638+ if detail not in ("low" , "high" , "auto" , "original" ):
639+ detail = "auto"
640+ return ResponseInputImage (
641+ type = "input_image" ,
642+ file_id = content .file_id ,
643+ detail = detail ,
644+ )
645+
646+ filename = content .name or additional_properties .get ("filename" )
647+ detail = additional_properties .get ("detail" )
648+ if detail not in ("low" , "high" , "auto" ):
649+ detail = None
650+ return ResponseInputFile (
651+ type = "input_file" ,
652+ file_id = content .file_id ,
653+ filename = filename if isinstance (filename , str ) else None ,
654+ detail = detail ,
655+ )
656+
657+ return None
658+
535659 async def get_item (self , conversation_id : str , item_id : str ) -> ConversationItem | None :
536660 """Get a specific conversation item by ID.
537661
0 commit comments