diff --git a/instructor/function_calls.py b/instructor/function_calls.py index 9b34e13e6..b8e70ea37 100644 --- a/instructor/function_calls.py +++ b/instructor/function_calls.py @@ -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" @@ -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)