@@ -102,36 +102,42 @@ def _format_antropic(self, chunk: str) -> str:
102
102
cleaned_chunk = chunk .split ("data:" )[1 ].strip ()
103
103
try :
104
104
chunk_dict = json .loads (cleaned_chunk )
105
- msg_type = chunk_dict .get ("type" , "" )
106
-
107
- finish_reason = None
108
- if msg_type == "message_stop" :
109
- finish_reason = "stop"
110
-
111
- # In type == "content_block_start" the content comes in "content_block"
112
- # In type == "content_block_delta" the content comes in "delta"
113
- msg_content_dict = chunk_dict .get ("delta" , {}) or chunk_dict .get ("content_block" , {})
114
- # We couldn't obtain the content from the chunk. Skip it.
115
- if not msg_content_dict :
116
- return ""
117
-
118
- msg_content = msg_content_dict .get ("text" , "" )
119
- open_ai_chunk = ModelResponse (
120
- id = f"anthropic-chat-{ str (uuid .uuid4 ())} " ,
121
- model = "anthropic-muxed-model" ,
122
- object = "chat.completion.chunk" ,
123
- choices = [
124
- StreamingChoices (
125
- finish_reason = finish_reason ,
126
- index = 0 ,
127
- delta = Delta (content = msg_content , role = "assistant" ),
128
- logprobs = None ,
129
- )
130
- ],
131
- )
105
+ except Exception as e :
106
+ logger .warning (f"Error parsing Anthropic chunk: { chunk } . Error: { e } " )
107
+ return cleaned_chunk .strip ()
108
+
109
+ msg_type = chunk_dict .get ("type" , "" )
110
+
111
+ finish_reason = None
112
+ if msg_type == "message_stop" :
113
+ finish_reason = "stop"
114
+
115
+ # In type == "content_block_start" the content comes in "content_block"
116
+ # In type == "content_block_delta" the content comes in "delta"
117
+ msg_content_dict = chunk_dict .get ("delta" , {}) or chunk_dict .get ("content_block" , {})
118
+ # We couldn't obtain the content from the chunk. Skip it.
119
+ if not msg_content_dict :
120
+ return ""
121
+ msg_content = msg_content_dict .get ("text" , "" )
122
+
123
+ open_ai_chunk = ModelResponse (
124
+ id = f"anthropic-chat-{ str (uuid .uuid4 ())} " ,
125
+ model = "anthropic-muxed-model" ,
126
+ object = "chat.completion.chunk" ,
127
+ choices = [
128
+ StreamingChoices (
129
+ finish_reason = finish_reason ,
130
+ index = 0 ,
131
+ delta = Delta (content = msg_content , role = "assistant" ),
132
+ logprobs = None ,
133
+ )
134
+ ],
135
+ )
136
+
137
+ try :
132
138
return open_ai_chunk .model_dump_json (exclude_none = True , exclude_unset = True )
133
139
except Exception as e :
134
- logger .warning (f"Error formatting Anthropic chunk: { chunk } . Error: { e } " )
140
+ logger .warning (f"Error serializing Anthropic chunk: { chunk } . Error: { e } " )
135
141
return cleaned_chunk .strip ()
136
142
137
143
def _format_as_openai_chunk (self , formatted_chunk : str ) -> str :
0 commit comments