@@ -205,9 +205,7 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
205205 elif content_type == 'image' :
206206 source = content_block .get ('source' , {})
207207 if source .get ('type' ) == 'base64' :
208- media_type = source .get (
209- 'media_type' , 'image/png'
210- )
208+ media_type = source .get ('media_type' , 'image/png' )
211209 data = source .get ('data' , '' )
212210 converted_parts .append (
213211 {
@@ -229,68 +227,36 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
229227 elif content_type == 'document' :
230228 # Documents have no direct OpenAI equivalent;
231229 # convert to a text representation.
232- document_source = content_block .get (
233- 'source' , {}
234- )
235- document_title = content_block .get (
236- 'title' , 'Document'
237- )
238- document_context = content_block .get (
239- 'context' , ''
240- )
241- document_text = (
242- f'[Document: { document_title } ]'
243- )
230+ document_source = content_block .get ('source' , {})
231+ document_title = content_block .get ('title' , 'Document' )
232+ document_context = content_block .get ('context' , '' )
233+ document_text = f'[Document: { document_title } ]'
244234 if document_context :
245235 document_text += f'\n { document_context } '
246- if (
247- document_source .get ('type' ) == 'text'
248- and document_source .get ('data' )
249- ):
250- document_text += (
251- f'\n { document_source ["data" ]} '
252- )
253- converted_parts .append (
254- {'type' : 'text' , 'text' : document_text }
255- )
236+ if document_source .get ('type' ) == 'text' and document_source .get ('data' ):
237+ document_text += f'\n { document_source ["data" ]} '
238+ converted_parts .append ({'type' : 'text' , 'text' : document_text })
256239 elif content_type == 'search_result' :
257240 # Convert search results to a text
258241 # representation with source attribution.
259242 search_title = content_block .get ('title' , '' )
260243 search_url = content_block .get ('source' , '' )
261- search_content_blocks = content_block .get (
262- 'content' , []
263- )
244+ search_content_blocks = content_block .get ('content' , [])
264245 search_texts = []
265246 for search_block in search_content_blocks :
266- if (
267- isinstance (search_block , dict )
268- and search_block .get ('type' ) == 'text'
269- ):
270- search_texts .append (
271- search_block .get ('text' , '' )
272- )
247+ if isinstance (search_block , dict ) and search_block .get ('type' ) == 'text' :
248+ search_texts .append (search_block .get ('text' , '' ))
273249 search_body = '\n ' .join (search_texts )
274- search_text = (
275- f'[Search Result: { search_title } ]'
276- )
250+ search_text = f'[Search Result: { search_title } ]'
277251 if search_url :
278252 search_text += f'\n Source: { search_url } '
279253 if search_body :
280254 search_text += f'\n { search_body } '
281- converted_parts .append (
282- {'type' : 'text' , 'text' : search_text }
283- )
255+ converted_parts .append ({'type' : 'text' , 'text' : search_text })
284256
285257 # Flatten to string when only text parts are present
286- if all (
287- part .get ('type' ) == 'text'
288- for part in converted_parts
289- ):
290- tool_content = '\n ' .join (
291- part .get ('text' , '' )
292- for part in converted_parts
293- )
258+ if all (part .get ('type' ) == 'text' for part in converted_parts ):
259+ tool_content = '\n ' .join (part .get ('text' , '' ) for part in converted_parts )
294260 elif converted_parts :
295261 tool_content = converted_parts
296262 else :
0 commit comments