Skip to content

Update from_litellm type hints to properly return AsyncInstructor#1324

Merged
ivanleomk merged 3 commits into567-labs:mainfrom
jonchun:main
Mar 31, 2025
Merged

Update from_litellm type hints to properly return AsyncInstructor#1324
ivanleomk merged 3 commits into567-labs:mainfrom
jonchun:main

Conversation

@jonchun
Copy link
Contributor

@jonchun jonchun commented Jan 28, 2025

Type Checking was previously failing when attempting to create an AsyncInstructor because litellm.acompletion is actually of type Callable[..., Awaitable[Any]] and not Awaitable. This was causing the type hinter to match on Callable[..., Any]. With these changes, the type hints properly return AsyncInstructor.

e.g.

from litellm import acompletion, completion
import instructor

client = instructor.from_litellm(completion) # (variable) client: Instructor
aclient = instructor.from_litellm(acompletion) # (variable) aclient: AsyncInstructor

Important

Update from_litellm type hints to correctly return AsyncInstructor for async completions.

  • Type Hints:
    • Update from_litellm overloads to use Callable[..., Awaitable[Any]] for async completions and Callable[..., Any] for sync completions.
    • Ensure from_litellm returns AsyncInstructor for async completions and Instructor for sync completions.

This description was created by Ellipsis for acf2563. It will automatically update as commits are pushed.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Reviewed everything up to acf2563 in 41 seconds

More details
  • Looked at 33 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 2 drafted comments based on config settings.
1. instructor/client.py:561
  • Draft comment:
    Using inspect.iscoroutinefunction may not cover all asynchronous cases, such as coroutine objects. Consider using asyncio.iscoroutinefunction or checking for Awaitable to ensure accurate type inference.
  • Reason this comment was not posted:
    Confidence changes required: 80%
    The PR correctly updates the type hints for the from_litellm function to distinguish between synchronous and asynchronous completions. However, the implementation of from_litellm uses inspect.iscoroutinefunction to determine if the function is asynchronous, which may not cover all cases, such as when the function is a coroutine but not defined with async def. This could lead to incorrect type inference.
2. instructor/client.py:544
  • Draft comment:
    The overloads for from_litellm are inconsistent with the implementation. The first overload should be for Callable[..., Any] returning Instructor, and the second for Callable[..., Awaitable[Any]] returning AsyncInstructor. This matches the logic in the implementation where inspect.iscoroutinefunction determines the return type.
  • Reason this comment was not posted:
    Comment looked like it was already resolved.

Workflow ID: wflow_QjN2oTmrAnnuQ7wU


You can customize Ellipsis with 👍 / 👎 feedback, review rules, user-specific overrides, quiet mode, and more.

@jonchun
Copy link
Contributor Author

jonchun commented Jan 28, 2025

Forgot to mention that the actual return types were correct, it was just the type hints that were incorrect.

is_async = inspect.iscoroutinefunction(completion)

correctly identifies that acompletion is an async function.

Copy link
Contributor

@ivanleomk ivanleomk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, approved and merging in

@ivanleomk ivanleomk merged commit 77e98e3 into 567-labs:main Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants