Skip to content

chore: change crew ai imports to be lazy#477

Merged
fernandocorreia-galileo merged 2 commits intomainfrom
bug/sc-51038/-tests-crewai-import-side-effects-cause-pytest
Feb 20, 2026
Merged

chore: change crew ai imports to be lazy#477
fernandocorreia-galileo merged 2 commits intomainfrom
bug/sc-51038/-tests-crewai-import-side-effects-cause-pytest

Conversation

@Focadecombate
Copy link
Contributor

@Focadecombate Focadecombate commented Feb 12, 2026

User description

Shortcut: sc-51038

Description:

CrewAI's crewai/llm.py wraps sys.stdout/sys.stderr with a FilteredStream class at
import time. When galileo/handlers/crewai/handler.py imports 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 file spam because
FilteredStream.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:

    • Replaced module-level crewai/litellm imports with a lazy
      _resolve_crewai_imports() function, guarded by a _crewai_imports_resolved flag and
      called once from __init__
    • Changed CrewAIEventListener class base from BaseEventListener to plain object,
      with dynamic __bases__ grafting after lazy import so super().__init__() and
      isinstance() still work
    • Moved event type imports into setup_listeners() (crewai is guaranteed in
      sys.modules by then)
    • Replaced quoted event type annotations (e.g., "CrewKickoffStartedEvent") with Any
      to resolve F821 lint errors
  • tests/test_crewai_handler.py:

    • Added patch("galileo.handlers.crewai.handler._crewai_imports_resolved", True) to 3
      test locations (crewai_callback fixture, test_initialization_with_crewai_available,
      test_initialization_with_crewai_unavailable) to prevent the lazy import function from
      running during tests

Verification

  • Importing the handler module no longer triggers any crewai import (assert 'crewai' not in sys.modules)
  • All 70 existing tests pass
  • Ruff lint clean

Tests:

  • Unit Tests Added
  • E2E Test Added (if it's a user-facing
    feature, or fixing a bug)

Generated description

Below is a concise technical summary of the changes proposed in this PR:
Implements lazy loading for crewai and litellm dependencies to prevent global side effects from interfering with parallel test execution. This change ensures that CrewAIEventListener only triggers external imports upon instantiation, resolving worker hangs and I/O errors during teardown.

TopicDetails
Lazy Import Logic Defer module-level imports to _resolve_crewai_imports and dynamically update CrewAIEventListener.__bases__ to maintain inheritance without early side effects.
Modified files (1)
  • src/galileo/handlers/crewai/handler.py
Latest Contributors(2)
UserCommitDate
jimbobbennett@mac.comfix-Fixing-docstrings-369October 15, 2025
Focadecombatefeat-add-support-for-m...October 10, 2025
Test Infrastructure Update unit tests to mock the new lazy import state and prevent actual dependency resolution during test collection.
Modified files (1)
  • tests/test_crewai_handler.py
Latest Contributors(2)
UserCommitDate
vamaq@users.noreply.gi...chore-Add-support-for-...December 26, 2025
Focadecombatefeat-add-support-for-m...October 10, 2025
This pull request is reviewed by Baz. Review like a pro on (Baz).

@Focadecombate Focadecombate requested a review from a team as a code owner February 12, 2026 17:45
@codecov
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

❌ Patch coverage is 51.56250% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.85%. Comparing base (545073a) to head (f7fecaa).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/galileo/handlers/crewai/handler.py 51.56% 31 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@calebe94 calebe94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@fernandocorreia-galileo fernandocorreia-galileo enabled auto-merge (squash) February 20, 2026 22:02
@fernandocorreia-galileo fernandocorreia-galileo merged commit d1f987a into main Feb 20, 2026
31 of 32 checks passed
@fernandocorreia-galileo fernandocorreia-galileo deleted the bug/sc-51038/-tests-crewai-import-side-effects-cause-pytest branch February 20, 2026 22:07
@Focadecombate Focadecombate mentioned this pull request Feb 23, 2026
2 tasks
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.

3 participants