Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Use more targetted "try-except" for anthropic chunk parsing #1128

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions src/codegate/muxing/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down