-
-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instructor's response_model
doesn't typecheck
#260
Comments
The error is so basic that I wonder if instructor doesn't support typechecking. On the other hand, it does advertise IDE completion support thanks to being based on Pydantic. |
yeah usually I just do or set the return type of a function I should try harder to see if generics work better. Will work on this |
I don't think the culprit is with the inference of the type of |
ah got it, yeah i do a patch instead of subclassing the entire client, do you have any suggestions? I should probs spend some time on this when i have time. |
I'm not an expert on Python typing but I'd start fixing this issue: You see that the inferred type from For expressing the extension of Based on my type-checking experience from my previous life, ChatGPT's idea check out at first look. |
By the way, are you not using type-checking yourself? |
i dont use the strictest mode, this is super helpful I'll see if someone can help out with this |
Not an expert in typing theory as well (need to revise on concepts such as I take reference of the strictness from OpenAI, and PyTorch and use I can raise a PR to implement pre-commit hooks (or Sample $ mypy --no-pretty \
--color-output \
--strict \
--python-version=3.9 \
--config-file=.mypy.ini \
instructor/patch.py yields the following sample snippet of the result of using instructor/patch.py:53: error: Unsupported left operand type for + ("None") [operator]
instructor/patch.py:53: error: No overload variant of "__add__" of "list" matches argument type "str" [operator]
instructor/patch.py:53: note: Possible overload variants:
instructor/patch.py:53: note: def __add__(self, list[Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]], /) -> list[Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]]
instructor/patch.py:53: note: def [_S] __add__(self, list[_S], /) -> list[Union[_S, ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]]
instructor/patch.py:53: note: Left operand is of type "Union[str, None, list[Union[ChatCompletionContentPartTextParam, ChatCompletionContentPartImageParam]]]" |
For the sync client, I have written a simple wrapper class around the patched client that allows the rest of my code to avoid type issues. from typing import TypeVar, Generic, Type
import instructor
from openai import OpenAI
# Define a generic type for the response model
T = TypeVar("T")
class StructuredOpenAI(Generic[T]):
"""A wrapper class around instructor's patched OpenAI client.
This simple wrapper allows us to avoid type errors when using Instructor's patched OpenAI client.
"""
def __init__(self, openai_client: OpenAI):
self._client = instructor.patch(openai_client)
def create(
self,
response_model: Type[T],
max_retries: int = 1,
validation_context=None,
*args,
**kwargs,
) -> T:
return self._client.chat.completions.create( # type: ignore
response_model=response_model,
validation_context=validation_context,
max_retries=max_retries,
*args,
**kwargs,
) |
How do you use this class? Doesn't it make sense to patch the upstream, i.e. instructor itself with similar code structure? |
i don't hate it. |
Hi everyone! I'm new and thought it would be interesting to contribute by adding types (I've only had one open source contribution and it's adding types to urllib3). Reviewing the dependency graph I thought it would make sense to start with The remaining errors I'm seeing relate to
I have a draft PR here that I'm still working on, just wondering more broadly if these changes are helpful and if not, is there another area that makes more sense to add types to? Do we even want to add types? Thanks! |
Hi and welcome! If your changes would make the original error I reported go away, it would be hugely helpful. |
Well Taking a closer look with ChatGPT it does seem the double decorator provides the intended behavior when the I'll leave this open for @jxnl to opine, I'm not as familiar with the test coverage to know if something would break somewhere else. Adding type ignores seems like the way forward here, though it does feel like cheating (for now).
|
Ach so #372. Generics FTW |
@savarin happy to take contribs into |
Hi, just saw this thread. While this might be slightly tangential to the main issue, I'm interested in how you created this dependency graph. Could you share the method or tools you used for this? |
@quickpanda Sure I used pydeps ( |
New types umbrella issue here #390 |
Describe the bug
Instructor's patch of openai's
completions.create
doesn't typecheck when used withresponse_model
.To Reproduce
Use the sample code from the README and open a project in VSCode with pylance installed.
Expected behavior
The code type-checks.
Screenshots
The text was updated successfully, but these errors were encountered: