@@ -58,6 +58,48 @@ def format_anthropic_usage(usage_data: dict[str, Any], token_usage: dict[str, in
5858 },
5959 }
6060
61+ @staticmethod
62+ async def convert_openai_file_content_to_anthropic (
63+ msg : dict [str , Any ], allow_url_download : bool = False
64+ ) -> dict [str , Any ]:
65+ """Convert OpenAI file content to Anthropic file content"""
66+ # Only support pdf & plain text files for now
67+ file = msg ["file" ]
68+ file_data = file .get ("file_data" )
69+ if not file_data :
70+ raise InvalidCompletionRequestException (
71+ provider_name = "anthropic" , error = ValueError ("file_data is required for file content in anthropic" )
72+ )
73+
74+ if file_data .startswith ("data:" ):
75+ # Extract media type and base64 data, assume it's a pdf file and return it as a base64 document
76+ parts = file_data .split ("," , 1 )
77+ media_type = parts [0 ].split (":" )[1 ].split (";" )[0 ] # e.g., "application/pdf"
78+ base64_data = parts [1 ] # The actual base64 string without prefix
79+ if not media_type == "application/pdf" :
80+ raise InvalidCompletionRequestException (
81+ provider_name = "anthropic" , error = ValueError ("Only application/pdf files are supported for base64 file content in anthropic" )
82+ )
83+ return {
84+ "type" : "document" ,
85+ "source" : {
86+ "data" : base64_data ,
87+ "media_type" : media_type ,
88+ "type" : "base64" ,
89+ }
90+ }
91+ else :
92+ # Treat it as a plain text file
93+ return {
94+ "type" : "document" ,
95+ "source" : {
96+ "data" : file_data ,
97+ "media_type" : "text/plain" ,
98+ "type" : "text" ,
99+ }
100+ }
101+
102+
61103 @staticmethod
62104 async def convert_openai_image_content_to_anthropic (
63105 msg : dict [str , Any ], allow_url_download : bool = False
@@ -154,6 +196,10 @@ async def convert_openai_content_to_anthropic(
154196 result .append (
155197 await AnthropicAdapter .convert_openai_image_content_to_anthropic (msg , allow_url_download = allow_url_download )
156198 )
199+ elif _type == "file" :
200+ result .append (
201+ await AnthropicAdapter .convert_openai_file_content_to_anthropic (msg , allow_url_download = allow_url_download )
202+ )
157203 else :
158204 error_message = f"{ _type } is not supported"
159205 logger .error (error_message )
0 commit comments