Skip to content
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

Fix streamed list of union basemodels #1141

Closed
wants to merge 1 commit into from

Conversation

raghavpillai
Copy link

@raghavpillai raghavpillai commented Nov 4, 2024

Fixes Pydantic ValidationError when streaming model with a list of unions of basemodels.

Example:

class PopulatedRoad(BaseModel):
    steps: list[Union[Exit, Car]] = Field(
        ...,
        description="The steps of the road.",
    )

Before:
image

After:
image

Closes #1140


Important

Fixes Pydantic ValidationError for streaming models with lists of unions of BaseModels by updating _make_field_optional() in instructor/dsl/partial.py.

  • Behavior:
    • Fixes Pydantic ValidationError for streaming models with lists of unions of BaseModels in instructor/dsl/partial.py.
    • Adds None to Union options and sets default to None in _make_field_optional().
  • Functions:
    • Introduces _process_annotation() to handle nested annotations, especially for Union types.
    • Updates _make_field_optional() to handle Union types and other generics correctly.
  • Misc:
    • Imports Union in instructor/dsl/partial.py.

This description was created by Ellipsis for 481d881. 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 481d881 in 47 seconds

More details
  • Looked at 90 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 5 drafted comments based on config settings.
1. instructor/dsl/partial.py:57
  • Draft comment:
    Consider refactoring the repeated pattern of adding None to Union options to a helper function to improve readability and maintainability. This pattern appears in both _make_field_optional and _process_annotation.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The code has a repeated pattern for handling Union types and adding None to the options. This can be refactored to improve readability and maintainability.
2. instructor/dsl/partial.py:41
  • Draft comment:
    Consider if deepcopy is necessary for FieldInfo objects. If a shallow copy suffices, it could improve performance.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The code uses deepcopy on FieldInfo objects, which might be unnecessary and could impact performance. It's worth considering if a shallow copy would suffice.
3. instructor/dsl/partial.py:58
  • Draft comment:
    Review the use of # type: ignore comments to ensure they are necessary and safe. Consider addressing the root cause of type issues if possible. This applies to other instances of # type: ignore in the code as well.
  • Reason this comment was not posted:
    Confidence changes required: 50%
    The code uses type: ignore comments, which can hide potential type issues. It's better to address the root cause or ensure that these are truly safe to ignore.
4. instructor/dsl/partial.py:39
  • Draft comment:
    The function _make_field_optional is complex and should have comments explaining its logic, especially around handling generics and Union types. This applies to _process_annotation as well.
  • Reason this comment was not posted:
    Confidence changes required: 80%
    The function _make_field_optional and _process_annotation are complex and require comments for better understanding.
5. instructor/dsl/partial.py:78
  • Draft comment:
    The function _process_annotation is complex and should have comments explaining its logic, especially around handling Union types and BaseModel.
  • Reason this comment was not posted:
    Confidence changes required: 80%
    The function _process_annotation is complex and requires comments for better understanding.

Workflow ID: wflow_MENGYJmD9vlhFJm8


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

@jxnl
Copy link
Collaborator

jxnl commented Nov 23, 2024

this should ultimtely be handled by pydantic will wait for a fix then

@jxnl jxnl closed this Nov 23, 2024
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.

Streamed list of Unions of BaseModels results in ValidationError
2 participants