diff --git a/src/codegate/muxing/adapter.py b/src/codegate/muxing/adapter.py index e0678f97..98859de7 100644 --- a/src/codegate/muxing/adapter.py +++ b/src/codegate/muxing/adapter.py @@ -102,36 +102,42 @@ def _format_antropic(self, chunk: str) -> str: cleaned_chunk = chunk.split("data:")[1].strip() try: chunk_dict = json.loads(cleaned_chunk) - msg_type = chunk_dict.get("type", "") - - finish_reason = None - if msg_type == "message_stop": - finish_reason = "stop" - - # In type == "content_block_start" the content comes in "content_block" - # In type == "content_block_delta" the content comes in "delta" - msg_content_dict = chunk_dict.get("delta", {}) or chunk_dict.get("content_block", {}) - # We couldn't obtain the content from the chunk. Skip it. - if not msg_content_dict: - return "" - - msg_content = msg_content_dict.get("text", "") - open_ai_chunk = ModelResponse( - id=f"anthropic-chat-{str(uuid.uuid4())}", - model="anthropic-muxed-model", - object="chat.completion.chunk", - choices=[ - StreamingChoices( - finish_reason=finish_reason, - index=0, - delta=Delta(content=msg_content, role="assistant"), - logprobs=None, - ) - ], - ) + except Exception as e: + logger.warning(f"Error parsing Anthropic chunk: {chunk}. Error: {e}") + return cleaned_chunk.strip() + + msg_type = chunk_dict.get("type", "") + + finish_reason = None + if msg_type == "message_stop": + finish_reason = "stop" + + # In type == "content_block_start" the content comes in "content_block" + # In type == "content_block_delta" the content comes in "delta" + msg_content_dict = chunk_dict.get("delta", {}) or chunk_dict.get("content_block", {}) + # We couldn't obtain the content from the chunk. Skip it. + if not msg_content_dict: + return "" + msg_content = msg_content_dict.get("text", "") + + open_ai_chunk = ModelResponse( + id=f"anthropic-chat-{str(uuid.uuid4())}", + model="anthropic-muxed-model", + object="chat.completion.chunk", + choices=[ + StreamingChoices( + finish_reason=finish_reason, + index=0, + delta=Delta(content=msg_content, role="assistant"), + logprobs=None, + ) + ], + ) + + try: return open_ai_chunk.model_dump_json(exclude_none=True, exclude_unset=True) except Exception as e: - logger.warning(f"Error formatting Anthropic chunk: {chunk}. Error: {e}") + logger.warning(f"Error serializing Anthropic chunk: {chunk}. Error: {e}") return cleaned_chunk.strip() def _format_as_openai_chunk(self, formatted_chunk: str) -> str: