Skip to content

Commit

Permalink
Anthropic IncompleteOutputException was never triggered for tools and…
Browse files Browse the repository at this point in the history
… JSON modes (#848)

Co-authored-by: Neo <[email protected]>
  • Loading branch information
palako and neo-sagittal authored Jul 26, 2024
1 parent a8fa281 commit 4860ca8
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ def parse_anthropic_tools(
validation_context: Optional[dict[str, Any]] = None,
strict: Optional[bool] = None,
) -> BaseModel:
from anthropic.types import Message
if isinstance(completion, Message) and completion.stop_reason == 'max_tokens':
raise IncompleteOutputException(last_completion=completion)

# Anthropic returns arguments as a dict, dump to json for model validation below
tool_calls = [
json.dumps(c.input) for c in completion.content if c.type == "tool_use"
Expand All @@ -319,6 +323,9 @@ def parse_anthropic_json(

assert isinstance(completion, Message)

if completion.stop_reason == 'max_tokens':
raise IncompleteOutputException(last_completion=completion)

text = completion.content[0].text
extra_text = extract_json_from_codeblock(text)

Expand Down

0 comments on commit 4860ca8

Please sign in to comment.