-
Notifications
You must be signed in to change notification settings - Fork 16
refactor: Improve code modularization for maintainability and scalability #84
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?
refactor: Improve code modularization for maintainability and scalability #84
Conversation
Create langchain_oci/common/ package to consolidate duplicated code: - common/auth.py: Single source of truth for OCIAuthType enum and create_oci_client_kwargs() function that was duplicated across llms/, embeddings/, and chat_models/ modules (~75 lines each) - common/utils.py: Shared OCIUtils class with helper functions for tool call conversion, schema resolution, and type checking This change eliminates approximately 300 lines of duplicated authentication logic, improving maintainability and reducing the risk of divergent implementations across modules.
Create langchain_oci/chat_models/providers/ to separate concerns and improve code organization: - providers/base.py: Abstract Provider base class defining the interface for all OCI GenAI providers (15 abstract methods) - providers/cohere.py: CohereProvider implementation (~400 lines) handling Cohere-specific message formatting, tool calls, and responses - providers/generic.py: GenericProvider and MetaProvider implementations (~500 lines) for Meta Llama, xAI Grok, OpenAI, and Mistral models Previously, all provider logic was embedded in oci_generative_ai.py (1,738 lines). This extraction: - Enables isolated testing of each provider - Makes it easier to add new providers - Reduces cognitive load when reading individual files - Follows the Single Responsibility Principle
Modify existing modules to leverage the new shared infrastructure: chat_models/oci_generative_ai.py: - Reduced from 1,738 lines to 692 lines (60% reduction) - Import providers from new providers/ subpackage - Import OCIUtils from common/utils llms/oci_generative_ai.py: - Replace duplicated OCIAuthType with import from common/auth - Replace 50+ lines of auth logic with create_oci_client_kwargs() - Reduced from 402 to 352 lines embeddings/oci_generative_ai.py: - Replace duplicated OCIAuthType with import from common/auth - Replace 50+ lines of auth logic with create_oci_client_kwargs() - Reduced from 231 to 185 lines All existing functionality preserved with improved maintainability.
Add OCIAuthType to langchain_oci/__init__.py exports, allowing users to import directly from the package root: from langchain_oci import OCIAuthType This provides a cleaner API for users who need to reference the authentication type enum without knowing the internal module structure.
- Remove unused OCIAuthType imports from llms and embeddings modules - Fix line length violations (max 88 characters) - Apply proper import formatting per ruff/isort standards - Expand multiline imports for better readability
Use dict key access instead of .get() for required config values to satisfy mypy type checking for open() function arguments.
No Logic Changes - Pure Structural RefactoringThis PR contains zero logic changes. All modifications are organizational only. What was done:
What was NOT done:
Bug fix included:The only functional change was correcting Verification:
|
Summary
This PR addresses code organization and maintainability concerns in the langchain-oci package by introducing proper modularization patterns. As langchain-oracle matures into a production-grade library used by enterprise customers, maintaining high code quality standards becomes increasingly important for long-term sustainability.
Changes
1. Create shared
common/modulecommon/auth.py: ConsolidatesOCIAuthTypeenum and authentication logic that was duplicated across 4 filescommon/utils.py: Extracts shared utility functions (OCIUtils) used by multiple modules2. Extract provider implementations into
providers/subpackageproviders/base.py: AbstractProviderbase class defining the interfaceproviders/cohere.py: Cohere-specific implementationproviders/generic.py: Generic provider for Meta Llama, xAI Grok, OpenAI, Mistral3. Streamline main modules
chat_models/oci_generative_ai.py: Reduced from 1,738 to 692 lines (60% reduction)llms/oci_generative_ai.py: Reduced from 402 to 352 linesembeddings/oci_generative_ai.py: Reduced from 231 to 185 linesWhy This Matters
For a production-grade open source library, code quality directly impacts:
Before/After
oci_generative_ai.pylinesTest Plan
Breaking Changes
None. This is a purely internal refactoring with no changes to the public API.