Skip to content

Refact: Consolidate Azure OpenAI and OpenAI implementations#2403

Merged
danielaskdd merged 5 commits intoHKUDS:mainfrom
danielaskdd:azure-cot-handling
Nov 21, 2025
Merged

Refact: Consolidate Azure OpenAI and OpenAI implementations#2403
danielaskdd merged 5 commits intoHKUDS:mainfrom
danielaskdd:azure-cot-handling

Conversation

@danielaskdd
Copy link
Collaborator

Refactor: Consolidate Azure OpenAI and OpenAI implementations

Summary

This PR consolidates the Azure OpenAI and OpenAI LLM implementations by merging their core logic into a unified codebase, significantly reducing code duplication while maintaining full backward compatibility.

Problem

The azure_openai.py and openai.py modules contained nearly identical implementations (~250 lines of duplicated code), differing only in client initialization parameters. This led to:

  • Duplicated COT (Chain of Thought) processing logic
  • Duplicated error handling and retry mechanisms
  • Maintenance overhead requiring bug fixes in two places
  • Risk of implementation drift between the two modules

Solution

1. Extended openai.py with unified client creation

  • Modified create_openai_async_client() to support both OpenAI and Azure OpenAI clients
  • Added use_azure, azure_deployment, and api_version parameters

2. Unified core functions

  • Updated openai_complete_if_cache() to handle both OpenAI and Azure configurations
  • Updated openai_embed() with the same Azure support

3. Added Azure wrapper functions in openai.py

  • azure_openai_complete_if_cache() - Handles Azure-specific environment variables
  • azure_openai_complete() - Compatibility wrapper
  • azure_openai_embed() - Azure embedding wrapper

4. Simplified azure_openai.py to a thin compatibility layer

  • Reduced from 250+ lines to 23 lines (91% reduction)
  • Now only re-exports functions from openai.py
  • Maintains all existing import paths

Changes

Files Modified:

  • lightrag/llm/openai.py - Added Azure support and wrapper functions (+170 lines)
  • lightrag/llm/azure_openai.py - Simplified to compatibility layer (-227 lines)

Code Metrics:

  • Before: ~900 lines total (650 in openai.py + 250 in azure_openai.py)
  • After: ~843 lines total (820 in openai.py + 23 in azure_openai.py)
  • Net reduction: 57 lines of duplicated code eliminated

Backward Compatibility

100% backward compatible - All existing import statements continue to work:

# Both import paths work and reference the same function
from lightrag.llm.azure_openai import azure_openai_complete_if_cache
from lightrag.llm.openai import azure_openai_complete_if_cache

Testing

  • ✅ Verified import compatibility from both modules
  • ✅ Confirmed functions are identical objects (same memory reference)
  • ✅ Existing code using lightrag.llm.azure_openai imports works unchanged
  • ✅ No changes to function signatures or behavior

Benefits

  1. Single source of truth - COT logic, error handling, and retry mechanisms maintained in one place
  2. Automatic synchronization - Bug fixes and enhancements automatically apply to both implementations
  3. Reduced maintenance burden - Changes only need to be made once
  4. Cleaner architecture - Clear separation between implementation (openai.py) and compatibility layer (azure_openai.py)
  5. Easier testing - Shared logic can use shared test cases

Migration Guide

No migration needed! All existing code continues to work without any changes. The refactoring is completely transparent to users.

• Unified OpenAI/Azure client creation
• Azure module now re-exports functions
• Backward compatibility maintained
• Reduced code duplication
@danielaskdd
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

• Remove redundant @Retry decorator
• Call openai_embed.func directly
• Add detailed decorator documentation
• Prevent double parameter injection
• Fix EmbeddingFunc wrapping issues
@danielaskdd
Copy link
Collaborator Author

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

- Handle None client_configs case
- Merge configs with explicit params
- Override client_configs with params
- Use dict unpacking for client init
- Maintain parameter precedence
• Add missing parameters to wrappers
• Update docstrings for clarity
• Ensure API consistency
• Fix parameter forwarding
• Maintain backward compatibility
@danielaskdd
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Bravo.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielaskdd danielaskdd merged commit 021b637 into HKUDS:main Nov 21, 2025
4 checks passed
@danielaskdd danielaskdd deleted the azure-cot-handling branch November 21, 2025 11:46
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