-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: Implement conditional imports for all integration modules #480
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
Merged
Conversation
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
- Introduced a new test suite to validate that integration modules handle optional dependencies correctly. - Ensured modules can be imported when dependencies are missing and provide helpful error messages. - Verified that all integration modules exist and can be imported when dependencies are available. - Implemented comprehensive checks for availability flags and graceful import handling. - This addition prevents regressions in conditional import handling across all integrations.
…pic integration - Implemented conditional import handling for the `anthropic` library, allowing for graceful degradation when the library is not installed. - Added type hints for `anthropic` types using forward references to improve code clarity and maintainability. - Introduced an informative error message when the `anthropic` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Anthropic integration.
… integration - Implemented conditional import handling for the `openai` library, allowing for graceful degradation when the library is not installed. - Enhanced type hints using forward references for `openai` types to improve code clarity and maintainability. - Introduced informative error messages when the `openai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the OpenAI integration.
…l integration - Implemented conditional import handling for the `mistralai` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `mistralai` types to enhance code clarity and maintainability. - Introduced an informative error message when the `mistralai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Mistral integration.
…ntegration - Implemented conditional import handling for the `groq` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `groq` types to enhance code clarity and maintainability. - Introduced an informative error message when the `groq` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Groq integration.
… integration - Improved conditional import handling for the `openai` library, ensuring graceful degradation when the library is not installed. - Enhanced type hints using forward references for `openai` types to improve code clarity and maintainability. - Added an informative error message when the `openai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the OpenAI integration.
…gChain integration - Implemented conditional import handling for the `langchain` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `langchain` types to enhance code clarity and maintainability. - Introduced an informative error message when the `langchain` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the LangChain integration.
whoseoyster
approved these changes
Jul 10, 2025
…ional imports - Enhanced exception handling in the `run_integration_test` function by specifying `FileNotFoundError` and `OSError` in the exception clause, ensuring more precise error management. - This update prevents potential silent failures when attempting to unlink temporary files, improving the robustness of the integration tests for conditional imports.
Merged
stainless-app bot
pushed a commit
that referenced
this pull request
Jul 11, 2025
…480) * feat(tests): add integration tests for conditional imports in modules - Introduced a new test suite to validate that integration modules handle optional dependencies correctly. - Ensured modules can be imported when dependencies are missing and provide helpful error messages. - Verified that all integration modules exist and can be imported when dependencies are available. - Implemented comprehensive checks for availability flags and graceful import handling. - This addition prevents regressions in conditional import handling across all integrations. * feat(tracer): enhance conditional imports and type hinting for Anthropic integration - Implemented conditional import handling for the `anthropic` library, allowing for graceful degradation when the library is not installed. - Added type hints for `anthropic` types using forward references to improve code clarity and maintainability. - Introduced an informative error message when the `anthropic` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Anthropic integration. * feat(tracer): improve conditional imports and type hinting for OpenAI integration - Implemented conditional import handling for the `openai` library, allowing for graceful degradation when the library is not installed. - Enhanced type hints using forward references for `openai` types to improve code clarity and maintainability. - Introduced informative error messages when the `openai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the OpenAI integration. * feat(tracer): enhance conditional imports and type hinting for Mistral integration - Implemented conditional import handling for the `mistralai` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `mistralai` types to enhance code clarity and maintainability. - Introduced an informative error message when the `mistralai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Mistral integration. * feat(tracer): enhance conditional imports and type hinting for Groq integration - Implemented conditional import handling for the `groq` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `groq` types to enhance code clarity and maintainability. - Introduced an informative error message when the `groq` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Groq integration. * feat(tracer): enhance conditional imports and type hinting for OpenAI integration - Improved conditional import handling for the `openai` library, ensuring graceful degradation when the library is not installed. - Enhanced type hints using forward references for `openai` types to improve code clarity and maintainability. - Added an informative error message when the `openai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the OpenAI integration. * feat(langchain): enhance conditional imports and type hinting for LangChain integration - Implemented conditional import handling for the `langchain` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `langchain` types to enhance code clarity and maintainability. - Introduced an informative error message when the `langchain` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the LangChain integration. * fix(tests): improve exception handling in integration test for conditional imports - Enhanced exception handling in the `run_integration_test` function by specifying `FileNotFoundError` and `OSError` in the exception clause, ensuring more precise error management. - This update prevents potential silent failures when attempting to unlink temporary files, improving the robustness of the integration tests for conditional imports.
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.
Implement conditional imports for all integration modules
Summary
Converts all integration modules to use conditional imports, making external dependencies truly optional. Users can now install only the integrations they need without being forced to install all third-party libraries.
Problem Solved
Previously, importing any integration module would fail if the corresponding third-party library wasn't installed, even if users didn't intend to use that specific integration. This created poor UX and unnecessary dependency bloat.
Changes Made
Integration Modules Updated
openai_tracer.py
- OpenAI SDK supportasync_openai_tracer.py
- Async OpenAI SDK supportanthropic_tracer.py
- Anthropic SDK supportgroq_tracer.py
- Groq SDK supportmistral_tracer.py
- Mistral SDK supportlangchain_callback.py
- LangChain supportopenai_agents.py
- OpenAI Agents SDK support (already implemented)Implementation Pattern
Each module now follows a consistent conditional import pattern:
User Experience Improvements
Before
After
Key Features
1. Graceful Import Handling
TYPE_CHECKING
2. Availability Flags
HAVE_*
flags for programmatic detection3. Helpful Error Messages
4. Type Safety Preserved
Testing
Comprehensive testing ensures all modules:
Dependencies
No changes to
pyproject.toml
required - integration libraries were already correctly excluded from required dependencies. This change makes the existing dependency structure work as intended.Impact
Checklist
HAVE_*
) to all modules