Skip to content

Lazily re-export databricks_openai package root symbols#440

Open
mdbecker wants to merge 2 commits into
databricks:mainfrom
mdbecker:main
Open

Lazily re-export databricks_openai package root symbols#440
mdbecker wants to merge 2 commits into
databricks:mainfrom
mdbecker:main

Conversation

@mdbecker

@mdbecker mdbecker commented Jun 10, 2026

Copy link
Copy Markdown

Summary

This PR preserves the existing databricks_openai package-root API while resolving exported symbols lazily.

The main motivation is to make narrow package-root imports like this avoid loading unrelated package-root re-exports:

from databricks_openai import DatabricksOpenAI

DatabricksOpenAI is implemented in databricks_openai.utils.clients. It does not require the MCP toolkit, vector search retriever tool, or Unity Catalog toolkit modules to be imported. Before this change, importing DatabricksOpenAI from the package root also eagerly imported those unrelated package-root re-exports because they were imported at package import time.

After this change, each package-root export is imported only when that symbol is accessed. The public aliases in __all__ are unchanged.

This PR also updates the existing package-root import smoke check so it is collected and reported as a normal pytest test item.

Changes

  • Replace eager package-root imports in databricks_openai/__init__.py with lazy re-exports using _LAZY_EXPORTS and module-level __getattr__.

  • Preserve the existing public aliases in __all__.

  • Cache resolved aliases in module globals after first access.

  • Keep __dir__ aligned with module globals and __all__.

  • Update the docstring alias list to include aliases that were already exported before this PR.

  • Wrap the existing package-root import smoke assertions in test_imports.py in a test_ function so the smoke check is collected and reported as a normal pytest test item.

  • Add focused lazy-import regression tests covering:

    • import databricks_openai does not eagerly import MCP/vector/UC integration modules.
    • Accessing databricks_openai.DatabricksOpenAI loads the client module without importing unrelated integrations.

References

Testing

Validated with:

uv run --exact --group tests pytest --cov --cov-report=term tests/unit_tests
uv run --exact --group tests pytest tests/unit_tests/test_imports.py -q
uv run --exact --group tests pytest tests/unit_tests/test_lazy_imports.py
uv run ruff check src tests

@mdbecker

Copy link
Copy Markdown
Author

@jennsun @annzhang-db @kunyuchen this might be helpful in preventing issues like #433 moving forward

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