Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

devin-ai-integration[bot]
Copy link
Contributor

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 in src/crewai/utilities/converter.py. When the conversion process fails repeatedly and the agent parameter becomes None, the code tries to access agent.function_calling_llm without checking if agent is None first.

Solution

  • Added a None check for the agent parameter at the beginning of convert_with_instructions
  • When agent is None, the function now prints a clear error message and returns the original result
  • This follows the existing error handling pattern used elsewhere in the converter utility

Changes Made

  1. Fixed the NoneType error in convert_with_instructions function by adding proper None checking
  2. Added comprehensive tests covering all scenarios where the None agent issue could occur:

Testing

  • Added 4 new test functions in tests/utilities/test_converter.py
  • Tests verify that the function handles None agents gracefully without crashing
  • Tests confirm appropriate error messages are displayed
  • All tests follow existing patterns and use proper mocking

Backward Compatibility

This change is fully backward compatible. The fix only affects the error case where agent is None, 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)

- 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>
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comment for PR #3018

Overview

This pull request addresses a NoneType error in the convert_with_instructions function by implementing necessary null checks and error handling when the agent parameter is None. Additionally, comprehensive test coverage is included to ensure robustness surrounding the new changes.

Positive Aspects

  • Implemented Null Checks: The addition of null checks prevents potential runtime errors when accessing attributes of a None agent.
  • User Feedback: The error messages provide essential details for debugging, improving user experience.
  • Preservation of Existing Return Type: The function signature remains consistent, which ensures compatibility with other components of the codebase.

Suggestions for Improvement

1. Error Message Enhancement

Presently, the error message lacks context.

  • Current Code:
    content="Failed to convert text into a Pydantic model: No agent available for conversion. Using raw output instead.",
  • Suggested Improvement:
    content=f"Failed to convert text into a Pydantic model: No agent available for conversion. Using raw output instead. Model: {model.__name__}",
    Including the model name aids in understanding the context of the failure better.

2. Type Hinting

Improving the specificity of type hints can enhance code readability and maintainability:

  • Current Function Signature:
    def convert_with_instructions(
        result: Any,
        model: Type[BaseModel],
        is_function_call: bool,
        agent: Any,
        converter_cls: Optional[Type[Converter]] = None,
    ) -> Union[dict, BaseModel, str]:
  • Suggested Change:
    def convert_with_instructions(
        result: Any,
        model: Type[BaseModel],
        is_function_call: bool,
        agent: Optional[Agent],  # More specific type
        converter_cls: Optional[Type[Converter]] = None,
    ) -> Union[dict, BaseModel, str]:

3. Logging for Better Debugging

Instead of just printing, incorporate logging for better troubleshooting:

  • Current Structure:
    if agent is None:
        Printer().print(...)
        return result
  • Improvement Suggestion:
    if agent is None:
        logger.warning("Attempted conversion with None agent")
        Printer().print(...)
        return result
    Logging can significantly enhance the traceability of errors in production environments.

Test Improvements

Enhance Test Structure

  • It might be beneficial to organize tests more methodically:
    class TestConverterNoneAgent:
        @pytest.fixture
        def mock_printer(self):
            with patch("crewai.utilities.converter.Printer") as mock:
                yield mock

Use Parameterized Tests

This 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 Validation

Implementing 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

  1. Documentation: Including a detailed docstring for convert_with_instructions would clarify its function, especially regarding the handling of a None agent.
  2. Error Handling: Consider including debug-level logging and defining custom exceptions for various failure scenarios.
  3. Testing: Think about including tests for edge cases with different model types and consider performance testing for large inputs.
  4. Type Safety: Using TypeGuard and adding runtime validations could enhance type security in critical parameters.

Impact Analysis

This 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!

devin-ai-integration bot and others added 3 commits June 16, 2025 21:11
- 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>
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.

1 participant