diff --git a/.ruff.toml b/.ruff.toml index ccf250dbf..9ec021070 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -31,6 +31,8 @@ target-version = "py39" [lint] select = [ + # isort + "I", # bugbear rules "B", # remove unused imports diff --git a/examples/anthropic/run.py b/examples/anthropic/run.py index f3f09e12f..e967babb1 100644 --- a/examples/anthropic/run.py +++ b/examples/anthropic/run.py @@ -1,5 +1,6 @@ -from pydantic import BaseModel import anthropic +from pydantic import BaseModel + import instructor # Patching the Anthropics client with the instructor for enhanced capabilities diff --git a/examples/auto-ticketer/run.py b/examples/auto-ticketer/run.py index 5002ba45b..9810c961a 100644 --- a/examples/auto-ticketer/run.py +++ b/examples/auto-ticketer/run.py @@ -1,9 +1,10 @@ -import instructor -from openai import OpenAI - +from enum import Enum from typing import Optional + +from openai import OpenAI from pydantic import BaseModel, Field -from enum import Enum + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/avail/run.py b/examples/avail/run.py index fd99111a0..45174788f 100644 --- a/examples/avail/run.py +++ b/examples/avail/run.py @@ -1,9 +1,10 @@ -from pydantic import BaseModel, Field -from typing import Literal from collections.abc import Iterable from datetime import datetime, timedelta +from typing import Literal from openai import OpenAI +from pydantic import BaseModel, Field + import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/avail/run_mixtral.py b/examples/avail/run_mixtral.py index aa88f0324..612717672 100644 --- a/examples/avail/run_mixtral.py +++ b/examples/avail/run_mixtral.py @@ -1,9 +1,10 @@ import os -from pydantic import BaseModel, Field -from typing import Literal from datetime import datetime, timedelta +from typing import Literal from openai import OpenAI +from pydantic import BaseModel, Field + import instructor client = instructor.from_openai( diff --git a/examples/batch-classification/run-cache.py b/examples/batch-classification/run-cache.py index c9f349cd9..2b017cd94 100644 --- a/examples/batch-classification/run-cache.py +++ b/examples/batch-classification/run-cache.py @@ -1,9 +1,10 @@ -import instructor import asyncio +from enum import Enum from openai import AsyncOpenAI from pydantic import BaseModel, Field, field_validator -from enum import Enum + +import instructor client = instructor.from_openai(AsyncOpenAI(), mode=instructor.Mode.TOOLS) sem = asyncio.Semaphore(5) diff --git a/examples/batch-classification/run.py b/examples/batch-classification/run.py index dc8223ec4..eb9c6cb00 100644 --- a/examples/batch-classification/run.py +++ b/examples/batch-classification/run.py @@ -1,10 +1,11 @@ -import json -import instructor import asyncio +import json +from enum import Enum from openai import AsyncOpenAI from pydantic import BaseModel, Field, field_validator -from enum import Enum + +import instructor client = AsyncOpenAI() client = instructor.from_openai(client, mode=instructor.Mode.TOOLS) diff --git a/examples/batch-classification/run_langsmith.py b/examples/batch-classification/run_langsmith.py index 144bab3cf..392c7968d 100644 --- a/examples/batch-classification/run_langsmith.py +++ b/examples/batch-classification/run_langsmith.py @@ -1,12 +1,12 @@ -import instructor import asyncio +from enum import Enum from langsmith import traceable from langsmith.wrappers import wrap_openai - from openai import AsyncOpenAI from pydantic import BaseModel, Field, field_validator -from enum import Enum + +import instructor client = wrap_openai(AsyncOpenAI()) client = instructor.from_openai(client, mode=instructor.Mode.TOOLS) diff --git a/examples/caching/example_diskcache.py b/examples/caching/example_diskcache.py index aceb1767a..b35aaba85 100644 --- a/examples/caching/example_diskcache.py +++ b/examples/caching/example_diskcache.py @@ -1,11 +1,12 @@ import functools import inspect -import instructor -import diskcache -from openai import OpenAI, AsyncOpenAI +import diskcache +from openai import AsyncOpenAI, OpenAI from pydantic import BaseModel +import instructor + client = instructor.from_openai(OpenAI()) aclient = instructor.from_openai(AsyncOpenAI()) diff --git a/examples/caching/example_redis.py b/examples/caching/example_redis.py index 27bb3fe9a..6a7068a3a 100644 --- a/examples/caching/example_redis.py +++ b/examples/caching/example_redis.py @@ -1,10 +1,11 @@ -import redis import functools import inspect -import instructor -from pydantic import BaseModel +import redis from openai import OpenAI +from pydantic import BaseModel + +import instructor client = instructor.from_openai(OpenAI()) cache = redis.Redis("localhost") diff --git a/examples/caching/lru.py b/examples/caching/lru.py index 97f26afe2..1a2f881d3 100644 --- a/examples/caching/lru.py +++ b/examples/caching/lru.py @@ -1,7 +1,9 @@ -import instructor +import functools + from openai import OpenAI from pydantic import BaseModel -import functools + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/chain-of-density/chain_of_density.py b/examples/chain-of-density/chain_of_density.py index 862edf200..0c3cddce9 100644 --- a/examples/chain-of-density/chain_of_density.py +++ b/examples/chain-of-density/chain_of_density.py @@ -1,8 +1,9 @@ -from pydantic import BaseModel, Field, field_validator -import instructor import nltk -from openai import OpenAI import spacy +from openai import OpenAI +from pydantic import BaseModel, Field, field_validator + +import instructor client = instructor.from_openai(OpenAI()) nlp = spacy.load("en_core_web_sm") diff --git a/examples/chain-of-density/finetune.py b/examples/chain-of-density/finetune.py index d9d621ef2..50eed6406 100644 --- a/examples/chain-of-density/finetune.py +++ b/examples/chain-of-density/finetune.py @@ -1,10 +1,12 @@ -from openai import OpenAI -from chain_of_density import summarize_article import csv import logging -import instructor + +from chain_of_density import summarize_article +from openai import OpenAI from pydantic import BaseModel, Field +import instructor + logging.basicConfig(level=logging.INFO) client = instructor.from_openai(OpenAI()) diff --git a/examples/citation_with_extraction/citation_fuzzy_match.py b/examples/citation_with_extraction/citation_fuzzy_match.py index 195cca4f4..8ce090d96 100644 --- a/examples/citation_with_extraction/citation_fuzzy_match.py +++ b/examples/citation_with_extraction/citation_fuzzy_match.py @@ -1,8 +1,8 @@ -import instructor - from loguru import logger from openai import OpenAI -from pydantic import Field, BaseModel, FieldValidationInfo, model_validator +from pydantic import BaseModel, Field, FieldValidationInfo, model_validator + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/citation_with_extraction/diagram.py b/examples/citation_with_extraction/diagram.py index 87ed23c04..16346564f 100644 --- a/examples/citation_with_extraction/diagram.py +++ b/examples/citation_with_extraction/diagram.py @@ -1,5 +1,4 @@ import erdantic as erd - from citation_fuzzy_match import QuestionAnswer diagram = erd.create(QuestionAnswer) diff --git a/examples/citation_with_extraction/main.py b/examples/citation_with_extraction/main.py index 58af2c971..30e1ae64d 100644 --- a/examples/citation_with_extraction/main.py +++ b/examples/citation_with_extraction/main.py @@ -1,16 +1,16 @@ import json +import logging +import os from collections.abc import Iterable -from fastapi import FastAPI, Request, HTTPException + +from fastapi import FastAPI, HTTPException, Request from fastapi.params import Depends -from instructor import OpenAISchema +from openai import OpenAI from pydantic import BaseModel, Field from starlette.responses import StreamingResponse -import os import instructor -import logging - -from openai import OpenAI +from instructor import OpenAISchema from instructor.dsl.multitask import MultiTaskBase client = instructor.from_openai(OpenAI()) diff --git a/examples/citation_with_extraction/modal_main.py b/examples/citation_with_extraction/modal_main.py index 5452486c9..65d339d02 100644 --- a/examples/citation_with_extraction/modal_main.py +++ b/examples/citation_with_extraction/modal_main.py @@ -1,5 +1,5 @@ -from main import app import modal +from main import app stub = modal.Stub("rag-citation") diff --git a/examples/citations/run.py b/examples/citations/run.py index a47bd18d8..ee9cfe52c 100644 --- a/examples/citations/run.py +++ b/examples/citations/run.py @@ -1,4 +1,5 @@ from typing import Optional + from openai import OpenAI from pydantic import ( BaseModel, diff --git a/examples/classification/classifiy_with_validation.py b/examples/classification/classifiy_with_validation.py index 29f2a293c..29f3694e8 100644 --- a/examples/classification/classifiy_with_validation.py +++ b/examples/classification/classifiy_with_validation.py @@ -1,9 +1,10 @@ # pip install openai instructor -from pydantic import BaseModel, field_validator, Field import openai -import instructor +from pydantic import BaseModel, Field, field_validator from tqdm import tqdm +import instructor + client = instructor.from_openai(openai.OpenAI()) classes = { diff --git a/examples/classification/multi_prediction.py b/examples/classification/multi_prediction.py index 27d51a911..45f178a55 100644 --- a/examples/classification/multi_prediction.py +++ b/examples/classification/multi_prediction.py @@ -1,9 +1,10 @@ import enum -import instructor from openai import OpenAI from pydantic import BaseModel +import instructor + client = instructor.from_openai(OpenAI()) diff --git a/examples/classification/simple_prediction.py b/examples/classification/simple_prediction.py index cf67bd395..fd596594a 100644 --- a/examples/classification/simple_prediction.py +++ b/examples/classification/simple_prediction.py @@ -1,9 +1,10 @@ import enum -import instructor -from openai import OpenAI +from openai import OpenAI from pydantic import BaseModel +import instructor + client = instructor.from_openai(OpenAI()) diff --git a/examples/codegen-from-schema/create_fastapi_app.py b/examples/codegen-from-schema/create_fastapi_app.py index 56884829e..5ee1b1e2a 100644 --- a/examples/codegen-from-schema/create_fastapi_app.py +++ b/examples/codegen-from-schema/create_fastapi_app.py @@ -1,9 +1,10 @@ -import json import datetime -from pathlib import Path -from jinja2 import Template +import json import re +from pathlib import Path + from datamodel_code_generator import InputFileType, generate +from jinja2 import Template from pydantic import BaseModel APP_TEMPLATE_STR = '''# generated by instructor-codegen: diff --git a/examples/codegen-from-schema/run.py b/examples/codegen-from-schema/run.py index 44177a528..268ce573a 100644 --- a/examples/codegen-from-schema/run.py +++ b/examples/codegen-from-schema/run.py @@ -4,13 +4,13 @@ # api_path: /api/v1/extract_person # json_schema_path: ./input.json -import instructor - from fastapi import FastAPI -from pydantic import BaseModel from jinja2 import Template from models import ExtractPerson from openai import AsyncOpenAI +from pydantic import BaseModel + +import instructor aclient = instructor.apatch(AsyncOpenAI()) diff --git a/examples/cohere/cohere.py b/examples/cohere/cohere.py index d3aff0ec9..6a6ca4e00 100644 --- a/examples/cohere/cohere.py +++ b/examples/cohere/cohere.py @@ -1,7 +1,7 @@ import cohere -import instructor from pydantic import BaseModel, Field +import instructor # Patching the Cohere client with the instructor for enhanced capabilities client = instructor.from_cohere( diff --git a/examples/crm/run.py b/examples/crm/run.py index db931b923..aaa4a5214 100644 --- a/examples/crm/run.py +++ b/examples/crm/run.py @@ -1,7 +1,9 @@ from enum import Enum + +from openai import OpenAI from pydantic import BaseModel, Field + import instructor -from openai import OpenAI client = instructor.from_openai(OpenAI()) diff --git a/examples/distilations/three_digit_mul.py b/examples/distilations/three_digit_mul.py index 9c521ee09..51c270e52 100644 --- a/examples/distilations/three_digit_mul.py +++ b/examples/distilations/three_digit_mul.py @@ -1,6 +1,7 @@ import logging from pydantic import BaseModel, Field + from instructor import Instructions logging.basicConfig(level=logging.INFO) diff --git a/examples/distilations/three_digit_mul_dispatch.py b/examples/distilations/three_digit_mul_dispatch.py index 0c91a1b41..5f8dcd5d3 100644 --- a/examples/distilations/three_digit_mul_dispatch.py +++ b/examples/distilations/three_digit_mul_dispatch.py @@ -1,8 +1,9 @@ import logging from pydantic import BaseModel, Field -from instructor import Instructions + import instructor +from instructor import Instructions instructor.from_openai() diff --git a/examples/evals/eval.py b/examples/evals/eval.py index 999c64b15..488daf9aa 100644 --- a/examples/evals/eval.py +++ b/examples/evals/eval.py @@ -1,11 +1,12 @@ +import json from collections import Counter, defaultdict from enum import Enum +from pprint import pprint from typing import Any, Union + +import models as m import numpy as np -import json from pydantic import ValidationError -from pprint import pprint -import models as m class Status(Enum): diff --git a/examples/evals/models.py b/examples/evals/models.py index 326bdedaf..da6b79645 100644 --- a/examples/evals/models.py +++ b/examples/evals/models.py @@ -1,6 +1,7 @@ +from enum import Enum from typing import Optional + from pydantic import BaseModel, Field -from enum import Enum class SourceType(str, Enum): diff --git a/examples/extract-table/run_vision.py b/examples/extract-table/run_vision.py index 5994a8ca5..090ddc66d 100644 --- a/examples/extract-table/run_vision.py +++ b/examples/extract-table/run_vision.py @@ -1,17 +1,19 @@ -from openai import OpenAI from io import StringIO from typing import Annotated, Any + +import pandas as pd +from openai import OpenAI from pydantic import ( BaseModel, BeforeValidator, - PlainSerializer, InstanceOf, + PlainSerializer, WithJsonSchema, ) -import instructor -import pandas as pd from rich.console import Console +import instructor + console = Console() client = instructor.from_openai( client=OpenAI(), diff --git a/examples/extract-table/run_vision_langsmith.py b/examples/extract-table/run_vision_langsmith.py index 17ec99878..9e3f8e0d5 100644 --- a/examples/extract-table/run_vision_langsmith.py +++ b/examples/extract-table/run_vision_langsmith.py @@ -1,18 +1,19 @@ -from openai import OpenAI from io import StringIO from typing import Annotated, Any + +import pandas as pd +from langsmith import traceable +from langsmith.wrappers import wrap_openai +from openai import OpenAI from pydantic import ( BaseModel, BeforeValidator, - PlainSerializer, InstanceOf, + PlainSerializer, WithJsonSchema, ) -import instructor -import pandas as pd -from langsmith.wrappers import wrap_openai -from langsmith import traceable +import instructor client = wrap_openai(OpenAI()) client = instructor.from_openai(client, mode=instructor.function_calls.Mode.MD_JSON) diff --git a/examples/extract-table/run_vision_org_table.py b/examples/extract-table/run_vision_org_table.py index c0c85a09d..8805599af 100644 --- a/examples/extract-table/run_vision_org_table.py +++ b/examples/extract-table/run_vision_org_table.py @@ -1,17 +1,19 @@ -from openai import OpenAI from io import StringIO from typing import Annotated, Any + +import pandas as pd +from openai import OpenAI from pydantic import ( BaseModel, BeforeValidator, - PlainSerializer, InstanceOf, + PlainSerializer, WithJsonSchema, ) -import instructor -import pandas as pd from rich.console import Console +import instructor + console = Console() client = instructor.from_openai( client=OpenAI(), diff --git a/examples/extract-table/test.py b/examples/extract-table/test.py index 3ec708cf5..e0bdc0e72 100644 --- a/examples/extract-table/test.py +++ b/examples/extract-table/test.py @@ -1,6 +1,6 @@ +from openai import OpenAI from pydantic import BaseModel -from openai import OpenAI import instructor client = OpenAI() diff --git a/examples/extracting-pii/run.py b/examples/extracting-pii/run.py index 3a880ffb4..b335eaf2b 100644 --- a/examples/extracting-pii/run.py +++ b/examples/extracting-pii/run.py @@ -1,7 +1,7 @@ +from openai import OpenAI from pydantic import BaseModel import instructor -from openai import OpenAI client = instructor.from_openai(OpenAI()) diff --git a/examples/fastapi_app/main.py b/examples/fastapi_app/main.py index 0b630df05..94d3b0d5d 100644 --- a/examples/fastapi_app/main.py +++ b/examples/fastapi_app/main.py @@ -1,8 +1,9 @@ from fastapi import FastAPI -from instructor import OpenAISchema -import instructor.dsl as dsl from pydantic import BaseModel, Field +import instructor.dsl as dsl +from instructor import OpenAISchema + app = FastAPI(title="Example Application using instructor") diff --git a/examples/fastapi_app/script.py b/examples/fastapi_app/script.py index 590b2b388..744c679c9 100644 --- a/examples/fastapi_app/script.py +++ b/examples/fastapi_app/script.py @@ -1,7 +1,9 @@ -from instructor import OpenAISchema, dsl -from pydantic import Field import json +from pydantic import Field + +from instructor import OpenAISchema, dsl + class SearchQuery(OpenAISchema): query: str = Field( diff --git a/examples/fizzbuzz/run.py b/examples/fizzbuzz/run.py index c92ea52f5..fb63fe75d 100644 --- a/examples/fizzbuzz/run.py +++ b/examples/fizzbuzz/run.py @@ -1,6 +1,7 @@ from __future__ import annotations from openai import OpenAI + import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/gpt-engineer/generate.py b/examples/gpt-engineer/generate.py index 963438d62..d7fbaa155 100644 --- a/examples/gpt-engineer/generate.py +++ b/examples/gpt-engineer/generate.py @@ -1,7 +1,7 @@ -import instructor - from openai import OpenAI from pydantic import Field + +import instructor from instructor import OpenAISchema client = instructor.from_openai(OpenAI()) diff --git a/examples/gpt-engineer/refactor.py b/examples/gpt-engineer/refactor.py index e9d506a82..05d4e24eb 100644 --- a/examples/gpt-engineer/refactor.py +++ b/examples/gpt-engineer/refactor.py @@ -1,9 +1,9 @@ -import instructor - +from generate import Program from openai import OpenAI from pydantic import Field, parse_file_as + +import instructor from instructor import OpenAISchema -from generate import Program client = instructor.from_openai(OpenAI()) diff --git a/examples/groq/groq_example.py b/examples/groq/groq_example.py index 22e63b019..e06c2b39e 100644 --- a/examples/groq/groq_example.py +++ b/examples/groq/groq_example.py @@ -1,6 +1,8 @@ import os -from pydantic import BaseModel, Field + from groq import Groq +from pydantic import BaseModel, Field + import instructor diff --git a/examples/groq/groq_example2.py b/examples/groq/groq_example2.py index ffb59780c..f8e5137d4 100644 --- a/examples/groq/groq_example2.py +++ b/examples/groq/groq_example2.py @@ -1,6 +1,8 @@ import os -from pydantic import BaseModel + from groq import Groq +from pydantic import BaseModel + import instructor client = Groq( diff --git a/examples/knowledge-graph/run.py b/examples/knowledge-graph/run.py index ec42298e5..af927440d 100644 --- a/examples/knowledge-graph/run.py +++ b/examples/knowledge-graph/run.py @@ -1,9 +1,8 @@ -import instructor - from graphviz import Digraph -from pydantic import BaseModel, Field from openai import OpenAI +from pydantic import BaseModel, Field +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/knowledge-graph/run_stream.py b/examples/knowledge-graph/run_stream.py index f0899fcce..2a74bdfc9 100644 --- a/examples/knowledge-graph/run_stream.py +++ b/examples/knowledge-graph/run_stream.py @@ -1,11 +1,11 @@ -from openai import OpenAI -import instructor - -from graphviz import Digraph from typing import Optional +from graphviz import Digraph +from openai import OpenAI from pydantic import BaseModel, Field +import instructor + client = instructor.from_openai(OpenAI()) diff --git a/examples/learn-async/run.py b/examples/learn-async/run.py index bf5e0df84..4246ad4c6 100644 --- a/examples/learn-async/run.py +++ b/examples/learn-async/run.py @@ -1,10 +1,10 @@ -import time import asyncio +import time -import instructor -from pydantic import BaseModel from openai import AsyncOpenAI +from pydantic import BaseModel +import instructor client = instructor.apatch(AsyncOpenAI()) diff --git a/examples/logging/run.py b/examples/logging/run.py index e0177eb53..43b95ad09 100644 --- a/examples/logging/run.py +++ b/examples/logging/run.py @@ -1,9 +1,9 @@ -import instructor -import openai import logging +import openai from pydantic import BaseModel +import instructor # Set logging to DEBUG logging.basicConfig(level=logging.DEBUG) diff --git a/examples/match_language/run_v1.py b/examples/match_language/run_v1.py index 7d14873f7..b9177b504 100644 --- a/examples/match_language/run_v1.py +++ b/examples/match_language/run_v1.py @@ -1,7 +1,8 @@ +from langdetect import detect +from openai import AsyncOpenAI from pydantic import BaseModel + from instructor import patch -from openai import AsyncOpenAI -from langdetect import detect docs = map( lambda x: x.strip(), diff --git a/examples/match_language/run_v2.py b/examples/match_language/run_v2.py index 29c2a2c9b..b5b15f108 100644 --- a/examples/match_language/run_v2.py +++ b/examples/match_language/run_v2.py @@ -1,7 +1,8 @@ +from langdetect import detect +from openai import AsyncOpenAI from pydantic import BaseModel, Field + from instructor import patch -from openai import AsyncOpenAI -from langdetect import detect docs = map( lambda x: x.strip(), diff --git a/examples/mistral/mistral.py b/examples/mistral/mistral.py index cfe537aeb..18257797c 100644 --- a/examples/mistral/mistral.py +++ b/examples/mistral/mistral.py @@ -1,8 +1,10 @@ -from pydantic import BaseModel +import os + from mistralai.client import MistralClient +from pydantic import BaseModel + from instructor import from_mistral from instructor.function_calls import Mode -import os class UserDetails(BaseModel): diff --git a/examples/multi-actions/run.py b/examples/multi-actions/run.py index d748a5739..49d58f01f 100644 --- a/examples/multi-actions/run.py +++ b/examples/multi-actions/run.py @@ -1,9 +1,10 @@ -import instructor import enum - from typing import Optional -from pydantic import BaseModel, Field + from openai import OpenAI +from pydantic import BaseModel, Field + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/multiple_search_queries/diagram.py b/examples/multiple_search_queries/diagram.py index 4d8ba37e9..f35fca3f8 100644 --- a/examples/multiple_search_queries/diagram.py +++ b/examples/multiple_search_queries/diagram.py @@ -1,5 +1,4 @@ import erdantic as erd - from segment_search_queries import MultiSearch diagram = erd.create(MultiSearch) diff --git a/examples/multiple_search_queries/segment_search_queries.py b/examples/multiple_search_queries/segment_search_queries.py index a5df902c7..203ddb188 100644 --- a/examples/multiple_search_queries/segment_search_queries.py +++ b/examples/multiple_search_queries/segment_search_queries.py @@ -1,8 +1,9 @@ import enum -import instructor from openai import OpenAI -from pydantic import Field, BaseModel +from pydantic import BaseModel, Field + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/open_source_examples/openrouter.py b/examples/open_source_examples/openrouter.py index ea7ad7d6b..a49ace343 100644 --- a/examples/open_source_examples/openrouter.py +++ b/examples/open_source_examples/openrouter.py @@ -1,8 +1,10 @@ import os -import instructor +from typing import Optional + from openai import OpenAI from pydantic import BaseModel, Field -from typing import Optional + +import instructor from instructor import Maybe, Mode # Extract API key from environment diff --git a/examples/open_source_examples/perplexity.py b/examples/open_source_examples/perplexity.py index b567ca5f2..3d1b3e284 100644 --- a/examples/open_source_examples/perplexity.py +++ b/examples/open_source_examples/perplexity.py @@ -1,8 +1,10 @@ import os -import instructor +from typing import Optional + from openai import OpenAI from pydantic import BaseModel, Field -from typing import Optional + +import instructor from instructor import Maybe, Mode # Extract API key from environment diff --git a/examples/open_source_examples/runpod.py b/examples/open_source_examples/runpod.py index da11fc843..467f622d7 100644 --- a/examples/open_source_examples/runpod.py +++ b/examples/open_source_examples/runpod.py @@ -1,8 +1,10 @@ import os -import instructor +from typing import Optional + from openai import OpenAI from pydantic import BaseModel, Field -from typing import Optional + +import instructor from instructor import Mode # Extract API key from environment diff --git a/examples/parallel/run.py b/examples/parallel/run.py index e3dadfcd7..9be35ac37 100644 --- a/examples/parallel/run.py +++ b/examples/parallel/run.py @@ -1,12 +1,13 @@ from __future__ import annotations -import openai -import instructor - -from typing import Literal from collections.abc import Iterable +from typing import Literal + +import openai from pydantic import BaseModel +import instructor + class Weather(BaseModel): location: str diff --git a/examples/partial_streaming/benchmark.py b/examples/partial_streaming/benchmark.py index 073d2ff3d..5bc618d81 100644 --- a/examples/partial_streaming/benchmark.py +++ b/examples/partial_streaming/benchmark.py @@ -2,11 +2,13 @@ # https://cookbook.openai.com/examples/how_to_stream_completions # https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb import time + import tiktoken -import instructor from openai import OpenAI from pydantic import BaseModel +import instructor + client = instructor.from_openai(OpenAI(), mode=instructor.Mode.MD_JSON) diff --git a/examples/partial_streaming/run.py b/examples/partial_streaming/run.py index 51ac46fdb..a025f6c5f 100644 --- a/examples/partial_streaming/run.py +++ b/examples/partial_streaming/run.py @@ -1,10 +1,11 @@ # Part of this code is adapted from the following examples from OpenAI Cookbook: # https://cookbook.openai.com/examples/how_to_stream_completions # https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb -import instructor from openai import OpenAI from pydantic import BaseModel +import instructor + client = instructor.from_openai(OpenAI(), mode=instructor.Mode.TOOLS) diff --git a/examples/patching/anyscale.py b/examples/patching/anyscale.py index 20d8079b2..b34db575c 100644 --- a/examples/patching/anyscale.py +++ b/examples/patching/anyscale.py @@ -1,9 +1,9 @@ import os -import instructor from openai import OpenAI from pydantic import BaseModel +import instructor # By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter client = instructor.from_openai( diff --git a/examples/patching/oai.py b/examples/patching/oai.py index 07499e170..d774b825e 100644 --- a/examples/patching/oai.py +++ b/examples/patching/oai.py @@ -1,8 +1,7 @@ -import instructor - from openai import OpenAI from pydantic import BaseModel +import instructor # By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter client = instructor.from_openai( diff --git a/examples/patching/pcalls.py b/examples/patching/pcalls.py index 85fad10db..a85aaa694 100644 --- a/examples/patching/pcalls.py +++ b/examples/patching/pcalls.py @@ -1,12 +1,12 @@ -from typing import Literal, Union +import time from collections.abc import Iterable -from pydantic import BaseModel -from instructor import OpenAISchema +from typing import Literal, Union -import time import openai -import instructor +from pydantic import BaseModel +import instructor +from instructor import OpenAISchema client = openai.OpenAI() diff --git a/examples/patching/together.py b/examples/patching/together.py index e38ab2ec0..7406023d7 100644 --- a/examples/patching/together.py +++ b/examples/patching/together.py @@ -1,6 +1,8 @@ import os + import openai from pydantic import BaseModel + import instructor client = openai.OpenAI( diff --git a/examples/query_planner_execution/diagram.py b/examples/query_planner_execution/diagram.py index b002a6ecc..50ef9ae5f 100644 --- a/examples/query_planner_execution/diagram.py +++ b/examples/query_planner_execution/diagram.py @@ -1,5 +1,4 @@ from erdantic import erd - from query_planner_execution import QueryPlan diagram = erd.create(QueryPlan) diff --git a/examples/query_planner_execution/query_planner_execution.py b/examples/query_planner_execution/query_planner_execution.py index b2a9f6695..dd6082d7c 100644 --- a/examples/query_planner_execution/query_planner_execution.py +++ b/examples/query_planner_execution/query_planner_execution.py @@ -1,9 +1,10 @@ import asyncio import enum -import instructor from openai import OpenAI -from pydantic import Field, BaseModel +from pydantic import BaseModel, Field + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/recursive_filepaths/diagram.py b/examples/recursive_filepaths/diagram.py index 178c3a417..37d6a7dfa 100644 --- a/examples/recursive_filepaths/diagram.py +++ b/examples/recursive_filepaths/diagram.py @@ -1,5 +1,4 @@ import erdantic as erd - from parse_recursive_paths import DirectoryTree diagram = erd.create(DirectoryTree) diff --git a/examples/recursive_filepaths/parse_recursive_paths.py b/examples/recursive_filepaths/parse_recursive_paths.py index 645bb515a..e7b011db1 100644 --- a/examples/recursive_filepaths/parse_recursive_paths.py +++ b/examples/recursive_filepaths/parse_recursive_paths.py @@ -1,9 +1,9 @@ import enum -import instructor from openai import OpenAI from pydantic import BaseModel, Field +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/resolving-complex-entities/run.py b/examples/resolving-complex-entities/run.py index 662738cb2..891ea5261 100644 --- a/examples/resolving-complex-entities/run.py +++ b/examples/resolving-complex-entities/run.py @@ -1,8 +1,8 @@ from graphviz import Digraph +from openai import OpenAI from pydantic import BaseModel, Field import instructor -from openai import OpenAI client = OpenAI() diff --git a/examples/retry/run.py b/examples/retry/run.py index ddd0ae354..f15ef4104 100644 --- a/examples/retry/run.py +++ b/examples/retry/run.py @@ -1,7 +1,8 @@ -from pydantic import BaseModel, field_validator +import tenacity from openai import OpenAI +from pydantic import BaseModel, field_validator + import instructor -import tenacity client = OpenAI() client = instructor.from_openai(client) diff --git a/examples/safer_sql_example/diagram.py b/examples/safer_sql_example/diagram.py index ae274ef93..31390a331 100644 --- a/examples/safer_sql_example/diagram.py +++ b/examples/safer_sql_example/diagram.py @@ -1,5 +1,4 @@ import erdantic as erd - from safe_sql import SQL diagram = erd.create(SQL) diff --git a/examples/safer_sql_example/safe_sql.py b/examples/safer_sql_example/safe_sql.py index 49f8b1874..45cc542ad 100644 --- a/examples/safer_sql_example/safe_sql.py +++ b/examples/safer_sql_example/safe_sql.py @@ -1,10 +1,11 @@ import enum -import instructor - from typing import Any + from openai import OpenAI from pydantic import BaseModel, Field +import instructor + client = instructor.from_openai(OpenAI()) diff --git a/examples/simple-extraction/maybe_user.py b/examples/simple-extraction/maybe_user.py index ea1844cbc..1000bb19f 100644 --- a/examples/simple-extraction/maybe_user.py +++ b/examples/simple-extraction/maybe_user.py @@ -1,8 +1,9 @@ -import instructor +from typing import Optional from openai import OpenAI from pydantic import BaseModel, Field -from typing import Optional + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/simple-extraction/user.py b/examples/simple-extraction/user.py index 20d057a18..c9f1e4803 100644 --- a/examples/simple-extraction/user.py +++ b/examples/simple-extraction/user.py @@ -1,8 +1,9 @@ -import instructor +from typing import Optional from openai import OpenAI from pydantic import BaseModel, Field -from typing import Optional + +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/sqlmodel/run.py b/examples/sqlmodel/run.py index 0e225e061..b65a35461 100644 --- a/examples/sqlmodel/run.py +++ b/examples/sqlmodel/run.py @@ -1,7 +1,9 @@ -import instructor -from openai import OpenAI from typing import Optional -from sqlmodel import Field, SQLModel, create_engine, Session + +from openai import OpenAI +from sqlmodel import Field, Session, SQLModel, create_engine + +import instructor # Define the model that will serve as a Table for the database diff --git a/examples/stream_action_items/run.py b/examples/stream_action_items/run.py index 6ae498e5d..e996f042c 100644 --- a/examples/stream_action_items/run.py +++ b/examples/stream_action_items/run.py @@ -1,11 +1,11 @@ -import instructor - -from pydantic import BaseModel, Field -from typing import Optional from collections.abc import Iterable +from typing import Optional + from openai import OpenAI +from pydantic import BaseModel, Field from rich.console import Console +import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/streaming_multitask/streaming_multitask.py b/examples/streaming_multitask/streaming_multitask.py index f26ab3c96..928d88bfb 100644 --- a/examples/streaming_multitask/streaming_multitask.py +++ b/examples/streaming_multitask/streaming_multitask.py @@ -1,12 +1,11 @@ import time - from collections.abc import Iterable + from openai import OpenAI from pydantic import BaseModel import instructor - client = instructor.from_openai(OpenAI()) diff --git a/examples/synethic-data/run.py b/examples/synethic-data/run.py index 897c494e4..683506560 100644 --- a/examples/synethic-data/run.py +++ b/examples/synethic-data/run.py @@ -1,8 +1,10 @@ -import openai -import instructor from collections.abc import Iterable + +import openai from pydantic import BaseModel, ConfigDict +import instructor + client = instructor.from_openai(openai.OpenAI()) diff --git a/examples/task_planner/diagram.py b/examples/task_planner/diagram.py index c4e73d8b1..3039045bc 100644 --- a/examples/task_planner/diagram.py +++ b/examples/task_planner/diagram.py @@ -1,5 +1,4 @@ import erdantic as erd - from task_planner_topological_sort import TaskPlan diagram = erd.create(TaskPlan) diff --git a/examples/task_planner/task_planner_topological_sort.py b/examples/task_planner/task_planner_topological_sort.py index 7625eb993..fdd7da531 100644 --- a/examples/task_planner/task_planner_topological_sort.py +++ b/examples/task_planner/task_planner_topological_sort.py @@ -14,8 +14,7 @@ from collections.abc import Generator from openai import OpenAI - -from pydantic import Field, BaseModel +from pydantic import BaseModel, Field import instructor diff --git a/examples/union/run.py b/examples/union/run.py index c370a101b..30acb20fe 100644 --- a/examples/union/run.py +++ b/examples/union/run.py @@ -1,7 +1,9 @@ -from pydantic import BaseModel, Field from typing import Union -import instructor + from openai import OpenAI +from pydantic import BaseModel, Field + +import instructor class Search(BaseModel): diff --git a/examples/validated-multiclass/run.py b/examples/validated-multiclass/run.py index 057a16620..1a9dbc8b8 100644 --- a/examples/validated-multiclass/run.py +++ b/examples/validated-multiclass/run.py @@ -1,7 +1,9 @@ -from pydantic import BaseModel, ValidationInfo, model_validator +import asyncio + import openai +from pydantic import BaseModel, ValidationInfo, model_validator + import instructor -import asyncio client = instructor.from_openai( openai.AsyncOpenAI(), diff --git a/examples/validators/allm_validator.py b/examples/validators/allm_validator.py index c1266e978..11c41ee69 100644 --- a/examples/validators/allm_validator.py +++ b/examples/validators/allm_validator.py @@ -1,8 +1,10 @@ import asyncio from typing import Annotated + +from openai import AsyncOpenAI from pydantic import BaseModel, BeforeValidator + from instructor import llm_validator, patch -from openai import AsyncOpenAI aclient = AsyncOpenAI() diff --git a/examples/validators/annotator.py b/examples/validators/annotator.py index cb8193cd5..5341971a6 100644 --- a/examples/validators/annotator.py +++ b/examples/validators/annotator.py @@ -1,4 +1,5 @@ from typing import Annotated + from pydantic import BaseModel, ValidationError from pydantic.functional_validators import AfterValidator diff --git a/examples/validators/chain_of_thought_validator.py b/examples/validators/chain_of_thought_validator.py index 8e014c7cc..a09f98f68 100644 --- a/examples/validators/chain_of_thought_validator.py +++ b/examples/validators/chain_of_thought_validator.py @@ -1,8 +1,9 @@ -import instructor -from openai import OpenAI +from typing import Optional +from openai import OpenAI from pydantic import BaseModel, Field, model_validator -from typing import Optional + +import instructor # Enables `response_model` and `max_retries` parameters client = instructor.from_openai(OpenAI()) diff --git a/examples/validators/citations.py b/examples/validators/citations.py index 1342728c6..fb11c0ee6 100644 --- a/examples/validators/citations.py +++ b/examples/validators/citations.py @@ -1,6 +1,8 @@ from typing import Annotated -from pydantic import BaseModel, ValidationError, ValidationInfo, AfterValidator + from openai import OpenAI +from pydantic import AfterValidator, BaseModel, ValidationError, ValidationInfo + import instructor client = instructor.from_openai(OpenAI()) diff --git a/examples/validators/competitors.py b/examples/validators/competitors.py index e3c5705ad..272ef32e5 100644 --- a/examples/validators/competitors.py +++ b/examples/validators/competitors.py @@ -1,6 +1,7 @@ from typing import Annotated -from pydantic import BaseModel, ValidationError, AfterValidator + from openai import OpenAI +from pydantic import AfterValidator, BaseModel, ValidationError import instructor diff --git a/examples/validators/just_a_guy.py b/examples/validators/just_a_guy.py index 3aaf7eece..c38cfd83c 100644 --- a/examples/validators/just_a_guy.py +++ b/examples/validators/just_a_guy.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, ValidationError, field_validator, ValidationInfo +from pydantic import BaseModel, ValidationError, ValidationInfo, field_validator class AnswerWithCitation(BaseModel): diff --git a/examples/validators/llm_validator.py b/examples/validators/llm_validator.py index 72fcc7af6..7d64b39b3 100644 --- a/examples/validators/llm_validator.py +++ b/examples/validators/llm_validator.py @@ -1,9 +1,10 @@ -import instructor +from typing import Annotated from openai import OpenAI +from pydantic import BaseModel, BeforeValidator, ValidationError + +import instructor from instructor import llm_validator -from pydantic import BaseModel, ValidationError, BeforeValidator -from typing import Annotated # Apply the patch to the OpenAI client client = instructor.from_openai(OpenAI()) diff --git a/examples/validators/moderation.py b/examples/validators/moderation.py index 6e021e1e8..9b38753bb 100644 --- a/examples/validators/moderation.py +++ b/examples/validators/moderation.py @@ -1,10 +1,10 @@ -import instructor - -from instructor import openai_moderation - from typing import Annotated -from pydantic import BaseModel, AfterValidator + from openai import OpenAI +from pydantic import AfterValidator, BaseModel + +import instructor +from instructor import openai_moderation client = instructor.from_openai(OpenAI()) diff --git a/examples/vision/run.py b/examples/vision/run.py index a3983eb27..9ff7defd5 100644 --- a/examples/vision/run.py +++ b/examples/vision/run.py @@ -1,7 +1,9 @@ -import instructor +import base64 + from openai import OpenAI from pydantic import BaseModel -import base64 + +import instructor client = instructor.from_openai(OpenAI(), mode=instructor.Mode.MD_JSON) @@ -18,9 +20,10 @@ def encode_image(image_path): def draw_circle(image_size, num_circles, path): - from PIL import Image, ImageDraw import random + from PIL import Image, ImageDraw + image = Image.new("RGB", image_size, "white") draw = ImageDraw.Draw(image) diff --git a/examples/vision/run_table.py b/examples/vision/run_table.py index 48af4e9c5..2b4827adf 100644 --- a/examples/vision/run_table.py +++ b/examples/vision/run_table.py @@ -1,16 +1,17 @@ from io import StringIO from typing import Annotated, Any + +import pandas as pd from openai import OpenAI from pydantic import ( BaseModel, BeforeValidator, - PlainSerializer, InstanceOf, + PlainSerializer, WithJsonSchema, ) -import pandas as pd -import instructor +import instructor client = instructor.from_openai(OpenAI(), mode=instructor.Mode.MD_JSON) diff --git a/examples/youtube-clips/run.py b/examples/youtube-clips/run.py index d5c506a69..de92e8e9b 100644 --- a/examples/youtube-clips/run.py +++ b/examples/youtube-clips/run.py @@ -1,8 +1,10 @@ -from youtube_transcript_api import YouTubeTranscriptApi -from pydantic import BaseModel, Field from collections.abc import Generator, Iterable -import instructor + import openai +from pydantic import BaseModel, Field +from youtube_transcript_api import YouTubeTranscriptApi + +import instructor client = instructor.from_openai(openai.OpenAI()) @@ -88,9 +90,9 @@ def yield_clips(segments: Iterable[TranscriptSegment]) -> Iterable[YoutubeClips] # Example usage if __name__ == "__main__": - from rich.table import Table from rich.console import Console from rich.prompt import Prompt + from rich.table import Table console = Console() url = Prompt.ask("Enter a YouTube URL") diff --git a/instructor/__init__.py b/instructor/__init__.py index 60b325d81..24787d7ac 100644 --- a/instructor/__init__.py +++ b/instructor/__init__.py @@ -1,27 +1,25 @@ import importlib.util -from .mode import Mode -from .process_response import handle_response_model +from .client import ( + AsyncInstructor, + Instructor, + Provider, + from_litellm, + from_openai, +) from .distil import FinetuneFormat, Instructions from .dsl import ( CitationMixin, + IterableModel, Maybe, Partial, - IterableModel, llm_validator, openai_moderation, ) from .function_calls import OpenAISchema, openai_schema +from .mode import Mode from .patch import apatch, patch -from .process_response import handle_parallel_model -from .client import ( - Instructor, - AsyncInstructor, - from_openai, - from_litellm, - Provider, -) - +from .process_response import handle_parallel_model, handle_response_model __all__ = [ "Instructor", diff --git a/instructor/cli/cli.py b/instructor/cli/cli.py index 7ff9e6f6f..8608b13a7 100644 --- a/instructor/cli/cli.py +++ b/instructor/cli/cli.py @@ -1,8 +1,9 @@ import typer -import instructor.cli.jobs as jobs + import instructor.cli.files as files -import instructor.cli.usage as usage import instructor.cli.hub as hub +import instructor.cli.jobs as jobs +import instructor.cli.usage as usage app = typer.Typer() diff --git a/instructor/cli/hub.py b/instructor/cli/hub.py index d0eddb6ef..dc2d881e5 100644 --- a/instructor/cli/hub.py +++ b/instructor/cli/hub.py @@ -1,12 +1,11 @@ from typing import Optional -import typer import httpx - +import typer from pydantic import BaseModel from rich.console import Console -from rich.table import Table from rich.markdown import Markdown +from rich.table import Table app = typer.Typer( name="hub", diff --git a/instructor/cli/jobs.py b/instructor/cli/jobs.py index 9a2c292ed..57be1be29 100644 --- a/instructor/cli/jobs.py +++ b/instructor/cli/jobs.py @@ -1,14 +1,14 @@ +import time +from datetime import datetime from typing import Optional, TypedDict -from openai import OpenAI -from openai.types.fine_tuning.job_create_params import Hyperparameters import typer -import time +from openai import OpenAI +from openai.types.fine_tuning import FineTuningJob +from openai.types.fine_tuning.job_create_params import Hyperparameters +from rich.console import Console from rich.live import Live from rich.table import Table -from rich.console import Console -from datetime import datetime -from openai.types.fine_tuning import FineTuningJob client = OpenAI() app = typer.Typer() diff --git a/instructor/cli/usage.py b/instructor/cli/usage.py index 0245094fb..ea7287c01 100644 --- a/instructor/cli/usage.py +++ b/instructor/cli/usage.py @@ -1,19 +1,19 @@ -from typing import Any, Union -from collections.abc import Awaitable -from datetime import datetime, timedelta -import typer -import os -import aiohttp import asyncio +import os from builtins import list as List from collections import defaultdict +from collections.abc import Awaitable +from datetime import datetime, timedelta +from typing import Any, Union + +import aiohttp +import typer from rich.console import Console -from rich.table import Table from rich.progress import Progress +from rich.table import Table from instructor._types._alias import ModelNames - app = typer.Typer() console = Console() diff --git a/instructor/client.py b/instructor/client.py index b21916ddd..a4ec7139c 100644 --- a/instructor/client.py +++ b/instructor/client.py @@ -1,22 +1,24 @@ from __future__ import annotations -import openai import inspect -import instructor -from .utils import Provider, get_provider -from openai.types.chat import ChatCompletionMessageParam +from collections.abc import AsyncGenerator, Awaitable, Generator, Iterable from typing import ( - TypeVar, + Any, Callable, - overload, + TypeVar, Union, - Any, + overload, ) -from collections.abc import Generator, Iterable, Awaitable, AsyncGenerator -from typing_extensions import Self + +import openai +from openai.types.chat import ChatCompletionMessageParam from pydantic import BaseModel +from typing_extensions import Self + +import instructor from instructor.dsl.partial import Partial +from .utils import Provider, get_provider T = TypeVar("T", bound=Union[BaseModel, "Iterable[Any]", "Partial[Any]"]) diff --git a/instructor/client_anthropic.py b/instructor/client_anthropic.py index f2900dc55..ea3f465b3 100644 --- a/instructor/client_anthropic.py +++ b/instructor/client_anthropic.py @@ -1,9 +1,10 @@ from __future__ import annotations +from typing import Any, overload + import anthropic -import instructor -from typing import overload, Any +import instructor @overload diff --git a/instructor/client_cohere.py b/instructor/client_cohere.py index d823b870c..ae27077ea 100644 --- a/instructor/client_cohere.py +++ b/instructor/client_cohere.py @@ -1,19 +1,20 @@ from __future__ import annotations -import cohere -import instructor from functools import wraps from typing import ( + Any, TypeVar, overload, ) -from typing import Any -from typing_extensions import ParamSpec + +import cohere from pydantic import BaseModel +from typing_extensions import ParamSpec + +import instructor from instructor.process_response import handle_response_model from instructor.retry import retry_async - T_Model = TypeVar("T_Model", bound=BaseModel) T_ParamSpec = ParamSpec("T_ParamSpec") diff --git a/instructor/client_groq.py b/instructor/client_groq.py index aeb25f83b..1b30869ae 100644 --- a/instructor/client_groq.py +++ b/instructor/client_groq.py @@ -1,8 +1,9 @@ from __future__ import annotations -from typing import overload, Any +from typing import Any, overload import groq + import instructor diff --git a/instructor/client_mistral.py b/instructor/client_mistral.py index a586dccbb..289c7ff22 100644 --- a/instructor/client_mistral.py +++ b/instructor/client_mistral.py @@ -1,10 +1,12 @@ # Future imports to ensure compatibility with Python 3.9 from __future__ import annotations -import mistralai.client +from typing import Any, overload + import mistralai.async_client as mistralaiasynccli +import mistralai.client + import instructor -from typing import overload, Any @overload diff --git a/instructor/distil.py b/instructor/distil.py index 16305ffdc..70fd1f472 100644 --- a/instructor/distil.py +++ b/instructor/distil.py @@ -1,28 +1,27 @@ import enum +import functools +import inspect import json -import uuid import logging -import inspect -import functools - +import uuid from typing import ( Any, Callable, + Literal, Optional, - TypeVar, TypedDict, - Literal, + TypeVar, Union, ) -from typing_extensions import ParamSpec, NotRequired + +from openai import OpenAI from openai.types.chat.chat_completion import ChatCompletion from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam from pydantic import BaseModel, validate_call +from typing_extensions import NotRequired, ParamSpec -from openai import OpenAI from instructor.function_calls import openai_schema - P = ParamSpec("P") T_Retval = TypeVar("T_Retval", bound=BaseModel) diff --git a/instructor/dsl/__init__.py b/instructor/dsl/__init__.py index 31b4d789b..92565a791 100644 --- a/instructor/dsl/__init__.py +++ b/instructor/dsl/__init__.py @@ -1,9 +1,9 @@ +from .citation import CitationMixin from .iterable import IterableModel from .maybe import Maybe from .partial import Partial +from .simple_type import ModelAdapter, is_simple_type from .validators import llm_validator, openai_moderation -from .citation import CitationMixin -from .simple_type import is_simple_type, ModelAdapter __all__ = [ # noqa: F405 "CitationMixin", diff --git a/instructor/dsl/citation.py b/instructor/dsl/citation.py index c5ccd4f04..5fa74e4b7 100644 --- a/instructor/dsl/citation.py +++ b/instructor/dsl/citation.py @@ -1,6 +1,7 @@ -from pydantic import BaseModel, Field, model_validator, ValidationInfo from collections.abc import Generator +from pydantic import BaseModel, Field, ValidationInfo, model_validator + class CitationMixin(BaseModel): """ diff --git a/instructor/dsl/iterable.py b/instructor/dsl/iterable.py index 8d325d7ad..c14a76943 100644 --- a/instructor/dsl/iterable.py +++ b/instructor/dsl/iterable.py @@ -1,7 +1,11 @@ -from typing import Any, Optional, cast, ClassVar from collections.abc import AsyncGenerator, Generator, Iterable +from typing import Any, ClassVar, Optional, cast -from pydantic import BaseModel, Field, create_model # type: ignore - remove once Pydantic is updated +from pydantic import ( # type: ignore - remove once Pydantic is updated + BaseModel, + Field, + create_model, +) from instructor.function_calls import OpenAISchema from instructor.mode import Mode diff --git a/instructor/dsl/maybe.py b/instructor/dsl/maybe.py index a3e92233f..fbcd46e18 100644 --- a/instructor/dsl/maybe.py +++ b/instructor/dsl/maybe.py @@ -1,6 +1,11 @@ -from pydantic import BaseModel, Field, create_model # type: ignore - remove once Pydantic is updated from typing import Generic, Optional, TypeVar +from pydantic import ( # type: ignore - remove once Pydantic is updated + BaseModel, + Field, + create_model, +) + T = TypeVar("T", bound=BaseModel) diff --git a/instructor/dsl/parallel.py b/instructor/dsl/parallel.py index 9fa17b1a3..90eed9c98 100644 --- a/instructor/dsl/parallel.py +++ b/instructor/dsl/parallel.py @@ -1,3 +1,4 @@ +from collections.abc import Generator, Iterable from typing import ( Any, Optional, @@ -6,11 +7,10 @@ get_args, get_origin, ) -from collections.abc import Generator + from pydantic import BaseModel -from instructor.function_calls import OpenAISchema, openai_schema -from collections.abc import Iterable +from instructor.function_calls import OpenAISchema, openai_schema from instructor.mode import Mode T = TypeVar("T", bound=OpenAISchema) diff --git a/instructor/dsl/partial.py b/instructor/dsl/partial.py index 759dbff47..1666f6457 100644 --- a/instructor/dsl/partial.py +++ b/instructor/dsl/partial.py @@ -8,21 +8,25 @@ from __future__ import annotations -import pydantic_core -from pydantic import BaseModel, create_model # type: ignore - remove once Pydantic is updated -from pydantic.fields import FieldInfo +from collections.abc import AsyncGenerator, Generator, Iterable +from copy import deepcopy +from functools import cache from typing import ( Any, Generic, - get_args, - get_origin, NoReturn, Optional, TypeVar, + get_args, + get_origin, ) -from collections.abc import AsyncGenerator, Generator, Iterable -from copy import deepcopy -from functools import cache + +import pydantic_core +from pydantic import ( # type: ignore - remove once Pydantic is updated + BaseModel, + create_model, +) +from pydantic.fields import FieldInfo from instructor.mode import Mode from instructor.utils import extract_json_from_stream, extract_json_from_stream_async diff --git a/instructor/dsl/simple_type.py b/instructor/dsl/simple_type.py index 110ff5b2c..8bcba713e 100644 --- a/instructor/dsl/simple_type.py +++ b/instructor/dsl/simple_type.py @@ -1,14 +1,17 @@ from __future__ import annotations -from inspect import isclass + import typing -from pydantic import BaseModel, create_model # type: ignore - remove once Pydantic is updated from enum import Enum +from inspect import isclass +from pydantic import ( # type: ignore - remove once Pydantic is updated + BaseModel, + create_model, +) from instructor.dsl.partial import Partial from instructor.function_calls import OpenAISchema - T = typing.TypeVar("T") diff --git a/instructor/dsl/validators.py b/instructor/dsl/validators.py index d6fa99c44..a5692b34e 100644 --- a/instructor/dsl/validators.py +++ b/instructor/dsl/validators.py @@ -3,8 +3,8 @@ from openai import OpenAI from pydantic import Field -from instructor.function_calls import OpenAISchema from instructor.client import Instructor +from instructor.function_calls import OpenAISchema class Validator(OpenAISchema): diff --git a/instructor/function_calls.py b/instructor/function_calls.py index abffe7124..366172302 100644 --- a/instructor/function_calls.py +++ b/instructor/function_calls.py @@ -4,11 +4,17 @@ from docstring_parser import parse from openai.types.chat import ChatCompletion -from pydantic import BaseModel, Field, TypeAdapter, ConfigDict, create_model # type: ignore - remove once Pydantic is updated +from pydantic import ( # type: ignore - remove once Pydantic is updated + BaseModel, + ConfigDict, + Field, + TypeAdapter, + create_model, +) + from instructor.exceptions import IncompleteOutputException from instructor.mode import Mode -from instructor.utils import extract_json_from_codeblock, classproperty - +from instructor.utils import classproperty, extract_json_from_codeblock T = TypeVar("T") diff --git a/instructor/patch.py b/instructor/patch.py index 9a1b2a0ae..300da8cc9 100644 --- a/instructor/patch.py +++ b/instructor/patch.py @@ -1,4 +1,6 @@ # type: ignore[all] +import logging +from collections.abc import Awaitable from functools import wraps from typing import ( Callable, @@ -7,19 +9,16 @@ Union, overload, ) -from collections.abc import Awaitable -from typing_extensions import ParamSpec from openai import AsyncOpenAI, OpenAI from pydantic import BaseModel +from typing_extensions import ParamSpec +from instructor.mode import Mode from instructor.process_response import handle_response_model from instructor.retry import retry_async, retry_sync from instructor.utils import is_async -from instructor.mode import Mode -import logging - logger = logging.getLogger("instructor") T_Model = TypeVar("T_Model", bound=BaseModel) diff --git a/instructor/process_response.py b/instructor/process_response.py index 16c24ad94..3a521f7b6 100644 --- a/instructor/process_response.py +++ b/instructor/process_response.py @@ -1,30 +1,29 @@ # type: ignore[all] from __future__ import annotations -from collections.abc import Iterable -from textwrap import dedent -from instructor.dsl.iterable import IterableBase, IterableModel -from instructor.dsl.parallel import ParallelBase, ParallelModel, handle_parallel_model -from instructor.dsl.partial import PartialBase -from instructor.dsl.simple_type import AdapterBase, ModelAdapter, is_simple_type -from instructor.function_calls import OpenAISchema, openai_schema -from instructor.utils import merge_consecutive_messages -from openai.types.chat import ChatCompletion -from pydantic import BaseModel - -import json import inspect +import json import logging +from collections.abc import Generator, Iterable +from textwrap import dedent from typing import ( + Any, + TypeVar, get_args, get_origin, - TypeVar, - Any, ) -from collections.abc import Generator + +from openai.types.chat import ChatCompletion +from pydantic import BaseModel from typing_extensions import ParamSpec +from instructor.dsl.iterable import IterableBase, IterableModel +from instructor.dsl.parallel import ParallelBase, ParallelModel, handle_parallel_model +from instructor.dsl.partial import PartialBase +from instructor.dsl.simple_type import AdapterBase, ModelAdapter, is_simple_type +from instructor.function_calls import OpenAISchema, openai_schema from instructor.mode import Mode +from instructor.utils import merge_consecutive_messages logger = logging.getLogger("instructor") diff --git a/instructor/retry.py b/instructor/retry.py index 0524ec93b..6b9528d0e 100644 --- a/instructor/retry.py +++ b/instructor/retry.py @@ -2,26 +2,23 @@ from __future__ import annotations import logging +from json import JSONDecodeError +from typing import Any, Callable, TypeVar from openai.types.chat import ChatCompletion +from openai.types.completion_usage import CompletionUsage +from pydantic import BaseModel, ValidationError +from tenacity import AsyncRetrying, RetryError, Retrying, stop_after_attempt +from typing_extensions import ParamSpec + from instructor.mode import Mode from instructor.process_response import process_response, process_response_async from instructor.utils import ( dump_message, - update_total_usage, merge_consecutive_messages, + update_total_usage, ) -from openai.types.completion_usage import CompletionUsage -from pydantic import ValidationError -from tenacity import AsyncRetrying, RetryError, Retrying, stop_after_attempt - - -from json import JSONDecodeError -from pydantic import BaseModel -from typing import Callable, TypeVar, Any -from typing_extensions import ParamSpec - logger = logging.getLogger("instructor") T_Model = TypeVar("T_Model", bound=BaseModel) diff --git a/instructor/utils.py b/instructor/utils.py index d5c0ce904..64221ea8d 100644 --- a/instructor/utils.py +++ b/instructor/utils.py @@ -3,24 +3,23 @@ import inspect import json import logging +from collections.abc import AsyncGenerator, Generator, Iterable from typing import ( + Any, Callable, Generic, Protocol, TypeVar, ) -from collections.abc import Generator, Iterable, AsyncGenerator -from typing import Callable, Protocol, TypeVar -from collections.abc import Generator, Iterable, AsyncGenerator -from openai.types.completion_usage import CompletionUsage + from anthropic.types import Usage as AnthropicUsage -from typing import Any from openai.types import CompletionUsage as OpenAIUsage from openai.types.chat import ( ChatCompletion, ChatCompletionMessage, ChatCompletionMessageParam, ) +from openai.types.completion_usage import CompletionUsage logger = logging.getLogger("instructor") R_co = TypeVar("R_co", covariant=True) diff --git a/tests/llm/test_new_client.py b/tests/llm/test_new_client.py index 5acf4e6a7..c7a2af687 100644 --- a/tests/llm/test_new_client.py +++ b/tests/llm/test_new_client.py @@ -1,11 +1,13 @@ -import cohere import os -import openai -import instructor + import anthropic +import cohere +import openai import pytest from pydantic import BaseModel, Field +import instructor + class User(BaseModel): name: str diff --git a/tests/llm/test_openai/conftest.py b/tests/llm/test_openai/conftest.py index 6039834da..497cb4084 100644 --- a/tests/llm/test_openai/conftest.py +++ b/tests/llm/test_openai/conftest.py @@ -1,8 +1,9 @@ # conftest.py -from openai import AsyncOpenAI, OpenAI -import pytest import os +import pytest +from openai import AsyncOpenAI, OpenAI + try: import braintrust diff --git a/tests/llm/test_openai/docs/test_docs.py b/tests/llm/test_openai/docs/test_docs.py index ab388c32a..6ca19f284 100644 --- a/tests/llm/test_openai/docs/test_docs.py +++ b/tests/llm/test_openai/docs/test_docs.py @@ -1,5 +1,5 @@ import pytest -from pytest_examples import find_examples, CodeExample, EvalExample +from pytest_examples import CodeExample, EvalExample, find_examples @pytest.mark.parametrize("example", find_examples("README.md"), ids=str) diff --git a/tests/llm/test_openai/docs/test_hub.py b/tests/llm/test_openai/docs/test_hub.py index afe9303af..be6fede92 100644 --- a/tests/llm/test_openai/docs/test_hub.py +++ b/tests/llm/test_openai/docs/test_hub.py @@ -1,5 +1,5 @@ import pytest -from pytest_examples import find_examples, CodeExample, EvalExample +from pytest_examples import CodeExample, EvalExample, find_examples @pytest.mark.parametrize("example", find_examples("docs/hub"), ids=str) diff --git a/tests/llm/test_openai/docs/test_mkdocs.py b/tests/llm/test_openai/docs/test_mkdocs.py index 1c83a1f13..4c139094e 100644 --- a/tests/llm/test_openai/docs/test_mkdocs.py +++ b/tests/llm/test_openai/docs/test_mkdocs.py @@ -1,4 +1,5 @@ import pathlib + import pytest diff --git a/tests/llm/test_openai/evals/test_classification_enums.py b/tests/llm/test_openai/evals/test_classification_enums.py index e42ec3305..2bd7dbb7c 100644 --- a/tests/llm/test_openai/evals/test_classification_enums.py +++ b/tests/llm/test_openai/evals/test_classification_enums.py @@ -2,11 +2,11 @@ from itertools import product import pytest -import instructor - from pydantic import BaseModel +import instructor from instructor.function_calls import Mode + from ..util import models, modes diff --git a/tests/llm/test_openai/evals/test_classification_literals.py b/tests/llm/test_openai/evals/test_classification_literals.py index 76252ab85..bde3ab3dc 100644 --- a/tests/llm/test_openai/evals/test_classification_literals.py +++ b/tests/llm/test_openai/evals/test_classification_literals.py @@ -2,11 +2,11 @@ from typing import Literal import pytest -import instructor - from pydantic import BaseModel +import instructor from instructor.function_calls import Mode + from ..util import models, modes diff --git a/tests/llm/test_openai/evals/test_entities.py b/tests/llm/test_openai/evals/test_entities.py index 7943fef53..67dd1c49a 100644 --- a/tests/llm/test_openai/evals/test_entities.py +++ b/tests/llm/test_openai/evals/test_entities.py @@ -1,10 +1,11 @@ from itertools import product -from pydantic import BaseModel, Field + import pytest +from pydantic import BaseModel, Field import instructor - from instructor.function_calls import Mode + from ..util import models, modes diff --git a/tests/llm/test_openai/evals/test_extract_users.py b/tests/llm/test_openai/evals/test_extract_users.py index a22a89b57..4590060fc 100644 --- a/tests/llm/test_openai/evals/test_extract_users.py +++ b/tests/llm/test_openai/evals/test_extract_users.py @@ -1,8 +1,11 @@ -import pytest from itertools import product + +import pytest from pydantic import BaseModel + import instructor from instructor.function_calls import Mode + from ..util import models, modes diff --git a/tests/llm/test_openai/evals/test_sentiment_analysis.py b/tests/llm/test_openai/evals/test_sentiment_analysis.py index 303e1dbd9..0782ea409 100644 --- a/tests/llm/test_openai/evals/test_sentiment_analysis.py +++ b/tests/llm/test_openai/evals/test_sentiment_analysis.py @@ -1,9 +1,12 @@ import enum from itertools import product -from pydantic import BaseModel + import pytest +from pydantic import BaseModel + import instructor from instructor.function_calls import Mode + from ..util import models, modes diff --git a/tests/llm/test_openai/test_modes.py b/tests/llm/test_openai/test_modes.py index 08b30a010..2d515b465 100644 --- a/tests/llm/test_openai/test_modes.py +++ b/tests/llm/test_openai/test_modes.py @@ -1,9 +1,10 @@ from itertools import product -from pydantic import BaseModel, Field import pytest +from pydantic import BaseModel, Field import instructor + from .util import models, modes diff --git a/tests/llm/test_openai/test_multitask.py b/tests/llm/test_openai/test_multitask.py index b6bb12b0f..ef46acc87 100644 --- a/tests/llm/test_openai/test_multitask.py +++ b/tests/llm/test_openai/test_multitask.py @@ -1,9 +1,11 @@ -from itertools import product from collections.abc import Iterable -from pydantic import BaseModel +from itertools import product + import pytest +from pydantic import BaseModel import instructor + from .util import models, modes diff --git a/tests/llm/test_openai/test_parallel.py b/tests/llm/test_openai/test_parallel.py index ed0f2fbf0..8f233db7d 100644 --- a/tests/llm/test_openai/test_parallel.py +++ b/tests/llm/test_openai/test_parallel.py @@ -1,10 +1,11 @@ from __future__ import annotations -from typing import Literal, Union from collections.abc import Iterable -from pydantic import BaseModel +from typing import Literal, Union import pytest +from pydantic import BaseModel + import instructor diff --git a/tests/llm/test_openai/test_patch.py b/tests/llm/test_openai/test_patch.py index be3db92a7..863039128 100644 --- a/tests/llm/test_openai/test_patch.py +++ b/tests/llm/test_openai/test_patch.py @@ -1,7 +1,9 @@ from itertools import product -from pydantic import BaseModel, field_validator -from openai.types.chat import ChatCompletion + import pytest +from openai.types.chat import ChatCompletion +from pydantic import BaseModel, field_validator + import instructor from .util import models, modes diff --git a/tests/llm/test_openai/test_retries.py b/tests/llm/test_openai/test_retries.py index b4297f9e1..fbf2b1a1a 100644 --- a/tests/llm/test_openai/test_retries.py +++ b/tests/llm/test_openai/test_retries.py @@ -1,8 +1,11 @@ +from itertools import product from typing import Annotated -from pydantic import AfterValidator, BaseModel, Field + import pytest +from pydantic import AfterValidator, BaseModel, Field + import instructor -from itertools import product + from .util import models, modes diff --git a/tests/llm/test_openai/test_simple_types.py b/tests/llm/test_openai/test_simple_types.py index adb256270..674ef0713 100644 --- a/tests/llm/test_openai/test_simple_types.py +++ b/tests/llm/test_openai/test_simple_types.py @@ -1,10 +1,11 @@ -import pytest -import instructor import enum - from typing import Annotated, Literal, Union + +import pytest from pydantic import Field +import instructor + @pytest.mark.asyncio async def test_response_simple_types(aclient): diff --git a/tests/llm/test_openai/test_stream.py b/tests/llm/test_openai/test_stream.py index 4110cc2c4..625966817 100644 --- a/tests/llm/test_openai/test_stream.py +++ b/tests/llm/test_openai/test_stream.py @@ -1,7 +1,9 @@ -from itertools import product from collections.abc import Iterable -from pydantic import BaseModel +from itertools import product + import pytest +from pydantic import BaseModel + import instructor from instructor.dsl.partial import Partial diff --git a/tests/llm/test_openai/test_validators.py b/tests/llm/test_openai/test_validators.py index 3b447b5b5..9ec273215 100644 --- a/tests/llm/test_openai/test_validators.py +++ b/tests/llm/test_openai/test_validators.py @@ -1,12 +1,12 @@ from itertools import product +from typing import Annotated + import pytest +from pydantic import AfterValidator, BaseModel, BeforeValidator, ValidationError import instructor - -from typing import Annotated -from pydantic import BaseModel, AfterValidator, BeforeValidator, ValidationError - from instructor.dsl.validators import llm_validator + from .util import models, modes diff --git a/tests/test_distil.py b/tests/test_distil.py index 7e3a3ab71..032ce3793 100644 --- a/tests/test_distil.py +++ b/tests/test_distil.py @@ -1,10 +1,10 @@ from typing import Any, Callable, cast -import pytest -import instructor +import pytest from openai import OpenAI from pydantic import BaseModel +import instructor from instructor.distil import ( Instructions, format_function, diff --git a/tests/test_function_calls.py b/tests/test_function_calls.py index 71dfe568d..2eca5c468 100644 --- a/tests/test_function_calls.py +++ b/tests/test_function_calls.py @@ -1,13 +1,13 @@ from typing import TypeVar + import pytest -from pydantic import BaseModel from openai.resources.chat.completions import ChatCompletion +from pydantic import BaseModel -from instructor import openai_schema, OpenAISchema import instructor +from instructor import OpenAISchema, openai_schema from instructor.exceptions import IncompleteOutputException - T = TypeVar("T") diff --git a/tests/test_simple_types.py b/tests/test_simple_types.py index 4add37bd1..9020bde0c 100644 --- a/tests/test_simple_types.py +++ b/tests/test_simple_types.py @@ -1,6 +1,7 @@ -from instructor.dsl import is_simple_type, Partial from pydantic import BaseModel +from instructor.dsl import Partial, is_simple_type + def test_enum_simple(): from enum import Enum @@ -28,9 +29,10 @@ class SampleModel(BaseModel): def test_annotated_simple(): - from pydantic import Field from typing import Annotated + from pydantic import Field + new_type = Annotated[int, Field(description="test")] assert is_simple_type(new_type), "Failed for type: " + str(new_type) diff --git a/tests/test_utils.py b/tests/test_utils.py index 217ce604d..15850d31d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,7 @@ import json + import pytest + from instructor.utils import ( classproperty, extract_json_from_codeblock,