Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/outlines/models/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ def generate(
**messages,
**inference_kwargs,
)
return completion.content[0].text
for block in completion.content:
if block.type == "text":
return block.text
raise ValueError("Anthropic response did not contain a text block.")

def generate_batch(
self,
Expand Down
21 changes: 21 additions & 0 deletions tests/models/test_anthropic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import io
from types import SimpleNamespace
from typing import Generator
from unittest.mock import MagicMock

from anthropic import Anthropic as AnthropicClient
from anthropic.types import TextBlock, ThinkingBlock
from PIL import Image as PILImage
import pytest

Expand Down Expand Up @@ -83,6 +86,24 @@ def __init__(self, foo):
model.generate("prompt", Foo(1))


def test_anthropic_response_uses_text_after_thinking_block():
client = MagicMock()
client.messages.create.return_value = SimpleNamespace(
content=[
ThinkingBlock(
signature="signature",
thinking="internal reasoning",
type="thinking",
),
TextBlock(text="final answer", type="text"),
]
)

model = Anthropic(client, MODEL_NAME)

assert model.generate("prompt", max_tokens=1) == "final answer"


@pytest.mark.api_call
def test_anthropic_simple_call(model):
result = model.generate("Respond with one word. Not more.", max_tokens=1024)
Expand Down