diff --git a/instructor/anthropic_utils.py b/instructor/anthropic_utils.py index 6bd75eee6..6aecd640c 100644 --- a/instructor/anthropic_utils.py +++ b/instructor/anthropic_utils.py @@ -1,9 +1,17 @@ +# type: ignore import re -import xmltodict from pydantic import BaseModel -import xml.etree.ElementTree as ET from typing import Type, Any, Dict, TypeVar + +try: + import xmltodict + import xml.etree.ElementTree as ET +except ImportError: + import warnings +warnings.warn("xmltodict and xml.etree.ElementTree modules not found. Please install them to proceed. `pip install xmltodict`", ImportWarning) + + T = TypeVar("T", bound=BaseModel) diff --git a/instructor/function_calls.py b/instructor/function_calls.py index 84a4da91b..d4fbf6dd2 100644 --- a/instructor/function_calls.py +++ b/instructor/function_calls.py @@ -2,14 +2,12 @@ from docstring_parser import parse from functools import wraps from pydantic import BaseModel, create_model -from instructor.exceptions import IncompleteOutputException from openai.types.chat import ChatCompletion +from instructor.exceptions import IncompleteOutputException from instructor.mode import Mode from instructor.utils import extract_json_from_codeblock import logging -import importlib -from .anthropic_utils import json_to_xml, extract_xml, xml_to_model T = TypeVar("T") @@ -63,6 +61,7 @@ def openai_schema(cls) -> Dict[str, Any]: @classmethod @property def anthropic_schema(cls) -> str: + from instructor.anthropic_utils import json_to_xml, extract_xml, xml_to_model return json_to_xml(cls) @classmethod @@ -87,10 +86,7 @@ def from_response( """ if mode == Mode.ANTHROPIC_TOOLS: try: - assert isinstance( - completion, - importlib.import_module("anthropic.types.message").Message, - ) + from instructor.anthropic_utils import extract_xml, xml_to_model except ImportError as err: raise ImportError("Please 'pip install anthropic' package to proceed.") from err assert hasattr(completion, "content") diff --git a/pyproject.toml b/pyproject.toml index 50d4c7551..bf7769f50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "instructor" -version = "0.6.5" +version = "0.6.6" description = "structured outputs for llm" authors = ["Jason Liu "] license = "MIT"