-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Fix NoneType error in convert_with_instructions when agent is None #3018
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
base: main
Are you sure you want to change the base?
Conversation
- Add None check for agent parameter before accessing attributes - Return original result with error message when agent is None - Add comprehensive tests covering None agent scenarios - Fixes GitHub issue #3017 Co-Authored-By: João <joao@crewai.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Disclaimer: This review was made by a crew of AI Agents. Code Review Comment for PR #3018OverviewThis pull request addresses a Positive Aspects
Suggestions for Improvement1. Error Message EnhancementPresently, the error message lacks context.
2. Type HintingImproving the specificity of type hints can enhance code readability and maintainability:
3. Logging for Better DebuggingInstead of just printing, incorporate logging for better troubleshooting:
Test ImprovementsEnhance Test Structure
Use Parameterized TestsThis promotes reusability: @pytest.mark.parametrize("input_data,expected", [
("Some text to convert", "Some text to convert"),
('{"name": "John", "age": "invalid_age"}', '{"name": "John", "age": "invalid_age"}'),
]) Error Message ValidationImplementing a test for verifying the error messages can come in handy: def test_error_message_format():
with patch("crewai.utilities.converter.Printer") as mock_printer:
convert_with_instructions("test", SimpleModel, False, None)
error_message = mock_printer.return_value.print.call_args[1]["content"]
assert "Failed to convert" in error_message
assert "No agent available" in error_message
assert mock_printer.return_value.print.call_args[1]["color"] == "red" General Recommendations
Impact AnalysisThis PR effectively addresses issue #3017 without introducing any breaking changes. The addition of user-friendly error messages and robust test coverage establishes a strong foundation for further development. In conclusion, the changes effectively enhance the functionality while maintaining stability. After addressing the suggested improvements, particularly around type hinting and logging, this pull request can be merged confidently. Let's keep up the great work! |
- Include model name in error messages for better context - Update all test cases to verify enhanced error messages - Add new test for error message format validation - Addresses suggestions from PR review by joaomdmoura Co-Authored-By: João <joao@crewai.com>
- Change agent parameter type from Any to Optional[Agent] for better type safety - Add TYPE_CHECKING import and Agent type import - Add Logger.log() call alongside Printer.print() for better debugging - Addresses remaining code review suggestions from joaomdmoura Co-Authored-By: João <joao@crewai.com>
- Python 3.12 has a known issue with pytest-recording where --block-network doesn't work - This causes tests to make real HTTP requests instead of using VCR cassettes - Use conditional logic to provide real OPENAI_API_KEY for Python 3.12 only - Other Python versions continue using fake-api-key as before - Addresses pytest-recording issue #150 affecting Python 3.12 CI environment Co-Authored-By: João <joao@crewai.com>
Fix NoneType error in convert_with_instructions when agent is None
Description
Fixes GitHub issue #3017 where a
'NoneType' object has no attribute 'function_calling_llm'
error occurs when CrewAI continuously fails to convert text to a Pydantic model.Root Cause
The issue occurs in the
convert_with_instructions
function at line 193-195 insrc/crewai/utilities/converter.py
. When the conversion process fails repeatedly and theagent
parameter becomesNone
, the code tries to accessagent.function_calling_llm
without checking ifagent
isNone
first.Solution
agent
parameter at the beginning ofconvert_with_instructions
agent
isNone
, the function now prints a clear error message and returns the original resultChanges Made
convert_with_instructions
function by adding proper None checkingconvert_with_instructions
with None agenthandle_partial_json
with None agentconvert_to_model
with None agent and invalid JSONTesting
tests/utilities/test_converter.py
Backward Compatibility
This change is fully backward compatible. The fix only affects the error case where
agent
isNone
, which previously would crash with a NoneType error. Now it gracefully handles the situation and returns the original result with a clear error message.Link to Devin run
https://app.devin.ai/sessions/cce4a82977bf457db0df15da6cf2bf6e
Requested by
João (joao@crewai.com)