-
-
Notifications
You must be signed in to change notification settings - Fork 931
fix(validation): fix unreachable allow_override code in llm_validator #2038
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
Open
Waqar53
wants to merge
2
commits into
567-labs:main
Choose a base branch
from
Waqar53:fix/llm-validator-allow-override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| """ | ||
| Tests for llm_validator allow_override functionality. | ||
|
|
||
| Tests cover: | ||
| - Valid input returns unchanged | ||
| - Invalid input with allow_override=True returns fixed_value | ||
| - Invalid input with allow_override=False raises ValueError | ||
| - Invalid input without fixed_value raises ValueError | ||
| """ | ||
|
|
||
| import pytest | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from instructor.validation.llm_validators import llm_validator | ||
| from instructor.processing.validators import Validator | ||
|
|
||
|
|
||
| class TestLLMValidatorAllowOverride: | ||
| """Tests for the allow_override parameter fix.""" | ||
|
|
||
| def test_valid_input_returns_unchanged(self): | ||
| """Test that valid input is returned unchanged.""" | ||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.create.return_value = Validator( | ||
| is_valid=True, | ||
| reason=None, | ||
| fixed_value=None, | ||
| ) | ||
|
|
||
| validator = llm_validator( | ||
| statement="must be lowercase", | ||
| client=mock_client, | ||
| allow_override=False, | ||
| ) | ||
|
|
||
| result = validator("hello") | ||
| assert result == "hello" | ||
|
|
||
| def test_invalid_with_override_returns_fixed_value(self): | ||
| """Test that invalid input with allow_override=True returns fixed_value.""" | ||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.create.return_value = Validator( | ||
| is_valid=False, | ||
| reason="Name should be lowercase", | ||
| fixed_value="jason liu", | ||
| ) | ||
|
|
||
| validator = llm_validator( | ||
| statement="must be lowercase", | ||
| client=mock_client, | ||
| allow_override=True, | ||
| ) | ||
|
|
||
| result = validator("Jason Liu") | ||
| assert result == "jason liu" | ||
|
|
||
| def test_invalid_without_override_raises_value_error(self): | ||
| """Test that invalid input with allow_override=False raises ValueError.""" | ||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.create.return_value = Validator( | ||
| is_valid=False, | ||
| reason="Name should be lowercase", | ||
| fixed_value="jason liu", | ||
| ) | ||
|
|
||
| validator = llm_validator( | ||
| statement="must be lowercase", | ||
| client=mock_client, | ||
| allow_override=False, | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="Name should be lowercase"): | ||
| validator("Jason Liu") | ||
|
|
||
| def test_invalid_with_override_but_no_fixed_value_raises(self): | ||
| """Test that invalid input without fixed_value raises ValueError even with override.""" | ||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.create.return_value = Validator( | ||
| is_valid=False, | ||
| reason="Invalid input, cannot fix", | ||
| fixed_value=None, | ||
| ) | ||
|
|
||
| validator = llm_validator( | ||
| statement="must be a valid email", | ||
| client=mock_client, | ||
| allow_override=True, | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match="Invalid input, cannot fix"): | ||
| validator("not-an-email") | ||
|
|
||
| def test_default_allow_override_is_false(self): | ||
| """Test that default behavior (allow_override=False) raises on invalid.""" | ||
| mock_client = MagicMock() | ||
| mock_client.chat.completions.create.return_value = Validator( | ||
| is_valid=False, | ||
| reason="Validation failed", | ||
| fixed_value="fixed", | ||
| ) | ||
|
|
||
| # Don't pass allow_override, use default | ||
| validator = llm_validator( | ||
| statement="must be valid", | ||
| client=mock_client, | ||
| ) | ||
|
|
||
| # Default should be False, so it should raise | ||
| with pytest.raises(ValueError): | ||
| validator("invalid") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pytest.main([__file__, "-v"]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.