chore: change crew ai imports to be lazy#477
Merged
fernandocorreia-galileo merged 2 commits intomainfrom Feb 20, 2026
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #477 +/- ##
==========================================
- Coverage 82.63% 81.85% -0.79%
==========================================
Files 96 96
Lines 9157 9164 +7
==========================================
- Hits 7567 7501 -66
- Misses 1590 1663 +73 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
2 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
User description
Shortcut: sc-51038
Description:
CrewAI's
crewai/llm.pywrapssys.stdout/sys.stderrwith aFilteredStreamclass atimport time. When
galileo/handlers/crewai/handler.pyimports crewai at module level,this side effect triggers during pytest test collection, causing pytest-xdist worker
teardown hangs and
ValueError: I/O operation on closed filespam becauseFilteredStream.flush()fails when the underlying stream is already closed.This PR defers all crewai/litellm imports from module-level to first use (inside
CrewAIEventListener.__init__), making the handler module side-effect-free on import.Changes
src/galileo/handlers/crewai/handler.py:crewai/litellmimports with a lazy_resolve_crewai_imports()function, guarded by a_crewai_imports_resolvedflag andcalled once from
__init__CrewAIEventListenerclass base fromBaseEventListenerto plainobject,with dynamic
__bases__grafting after lazy import sosuper().__init__()andisinstance()still worksetup_listeners()(crewai is guaranteed insys.modulesby then)"CrewKickoffStartedEvent") withAnyto resolve F821 lint errors
tests/test_crewai_handler.py:patch("galileo.handlers.crewai.handler._crewai_imports_resolved", True)to 3test locations (
crewai_callbackfixture,test_initialization_with_crewai_available,test_initialization_with_crewai_unavailable) to prevent the lazy import function fromrunning during tests
Verification
assert 'crewai' not in sys.modules)Tests:
feature, or fixing a bug)
Generated description
Below is a concise technical summary of the changes proposed in this PR:
Implements lazy loading for
crewaiandlitellmdependencies to prevent global side effects from interfering with parallel test execution. This change ensures thatCrewAIEventListeneronly triggers external imports upon instantiation, resolving worker hangs and I/O errors during teardown._resolve_crewai_importsand dynamically updateCrewAIEventListener.__bases__to maintain inheritance without early side effects.Modified files (1)
Latest Contributors(2)
Modified files (1)
Latest Contributors(2)