Skip to content

Commit 310e3b3

Browse files
committed
Fix mypy issues
1 parent 1c3ec18 commit 310e3b3

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,28 @@ def _translate_openai_content_to_anthropic(
439439
elif choice.message.thinking_blocks is not None:
440440
for thinking_block in choice.message.thinking_blocks:
441441
if "thinking" in thinking_block and "signature" in thinking_block:
442+
thinking = thinking_block.get("thinking")
443+
signature = thinking_block.get("signature")
444+
445+
assert isinstance(thinking, str)
446+
assert isinstance(signature, str) or signature is None
447+
442448
new_content.append(
443449
AnthropicResponseContentBlockThinking(
444450
type="thinking",
445-
thinking=thinking_block.get("thinking") or "",
446-
signature=thinking_block.get("signature") or "",
451+
thinking=thinking,
452+
signature=signature,
447453
)
448454
)
449455
elif "data" in thinking_block:
456+
data = thinking_block.get("data")
457+
458+
assert isinstance(data, str)
459+
450460
new_content.append(
451461
AnthropicResponseContentBlockRedactedThinking(
452462
type="redacted_thinking",
453-
data=thinking_block.get("data", ""),
463+
data=data,
454464
)
455465
)
456466

@@ -525,16 +535,22 @@ def _translate_streaming_openai_chunk_to_anthropic_content_block(
525535
):
526536
thinking_blocks = choice.delta.thinking_blocks or []
527537
if len(thinking_blocks) > 0:
528-
thinking = thinking_blocks[0].get("thinking") or ""
529-
signature = thinking_blocks[0].get("signature") or ""
530-
531-
if thinking and signature:
532-
raise ValueError("Both `thinking` and `signature` in a single streaming chunk isn't supported.")
533-
return "thinking", ChatCompletionThinkingBlock(
534-
type="thinking",
535-
thinking=thinking,
536-
signature=signature
537-
)
538+
thinking_block = thinking_blocks[0]
539+
if thinking_block["type"] == "thinking":
540+
thinking = thinking_block.get("thinking") or ""
541+
signature = thinking_block.get("signature") or ""
542+
543+
assert isinstance(thinking, str)
544+
assert isinstance(signature, str)
545+
546+
if thinking and signature:
547+
raise ValueError("Both `thinking` and `signature` in a single streaming chunk isn't supported.")
548+
549+
return "thinking", ChatCompletionThinkingBlock(
550+
type="thinking",
551+
thinking=thinking,
552+
signature=signature
553+
)
538554

539555

540556
return "text", TextBlock(type="text", text="")
@@ -564,8 +580,16 @@ def _translate_streaming_openai_chunk_to_anthropic(
564580
elif isinstance(choice, StreamingChoices) and hasattr(choice.delta, "thinking_blocks"):
565581
thinking_blocks = choice.delta.thinking_blocks or []
566582
if len(thinking_blocks) > 0:
567-
reasoning_content += thinking_blocks[0].get("thinking") or ""
568-
reasoning_signature += thinking_blocks[0].get("signature") or ""
583+
for thinking_block in thinking_blocks:
584+
if thinking_block["type"] == "thinking":
585+
thinking = thinking_block.get("thinking") or ""
586+
signature = thinking_block.get("signature") or ""
587+
588+
assert isinstance(thinking, str)
589+
assert isinstance(signature, str)
590+
591+
reasoning_content += thinking
592+
reasoning_signature += signature
569593

570594
if reasoning_content and reasoning_signature:
571595
raise ValueError("Both `reasoning` and `signature` in a single streaming chunk isn't supported.")

0 commit comments

Comments
 (0)