Skip to content

Make upstream provider SDKs optional#132

Merged
elprans merged 1 commit into
mainfrom
elprans/optional-sdks
May 14, 2026
Merged

Make upstream provider SDKs optional#132
elprans merged 1 commit into
mainfrom
elprans/optional-sdks

Conversation

@elprans
Copy link
Copy Markdown
Collaborator

@elprans elprans commented May 14, 2026

The ai package no longer depends on openai and anthropic directly
and instead loads the SDKs lazily when a corresponding provider is
instantiated. Upstream SDKs can be pulled-in via appropriately named
optional dependency groups.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 14, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ai-python Ready Ready Preview, Comment May 14, 2026 9:46pm

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes upstream provider SDKs (openai, anthropic) optional for the ai package by moving them to extras and switching provider implementations to import those SDKs lazily only when the corresponding provider is instantiated/used.

Changes:

  • Move openai/anthropic from required dependencies to optional extras (ai[openai], ai[anthropic]) and keep them available in the dev dependency group.
  • Add an InstallationError plus shared optional-import helper, and refactor OpenAI/Anthropic providers/protocols/errors to avoid importing SDKs at import time.
  • Add tests that assert a clear installation error is raised when a provider SDK is missing; update docs and example runners accordingly.

Reviewed changes

Copilot reviewed 20 out of 21 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
uv.lock Reflects dependency changes: SDKs moved to optional extras and included in dev group.
pyproject.toml Moves openai/anthropic to [project.optional-dependencies] and adds them to the dev dependency group.
src/ai/providers/_optional.py Adds shared helper to import optional SDK modules and raise a user-facing InstallationError.
src/ai/errors.py Introduces InstallationError in the public error hierarchy and exports it.
src/ai/init.py Re-exports InstallationError from the top-level ai package API.
src/ai/providers/openai/_sdk.py Centralizes lazy OpenAI SDK imports behind a typed protocol wrapper.
src/ai/providers/openai/provider.py Removes eager OpenAI SDK import; loads SDK via _sdk when needed.
src/ai/providers/openai/protocol.py Removes eager OpenAI SDK import; uses _sdk for runtime exception types and schema helper import.
src/ai/providers/openai/errors.py Removes eager OpenAI SDK import; maps errors using lazily imported SDK types.
src/ai/providers/anthropic/_sdk.py Centralizes lazy Anthropic SDK imports behind a typed protocol wrapper.
src/ai/providers/anthropic/provider.py Removes eager Anthropic SDK import; loads SDK via _sdk when needed.
src/ai/providers/anthropic/protocol.py Removes eager Anthropic SDK import; uses _sdk for runtime exception types.
src/ai/providers/anthropic/errors.py Removes eager Anthropic SDK import; maps errors using lazily imported SDK types.
tests/providers/openai/test_provider.py Adds coverage asserting InstallationError when OpenAI SDK cannot be imported.
tests/providers/anthropic/test_provider.py Adds coverage asserting InstallationError when Anthropic SDK cannot be imported.
README.md Documents optional extras required for direct OpenAI/Anthropic-compatible providers.
skills/ai/SKILL.md Updates skill docs to mention provider extras and gateway behavior.
examples/samples/explicit_client.py Moves explicit provider creation into main() to align with optional dependency behavior.
examples/samples/builtin_web_search.py Defers provider/model setup into main() and avoids sys.exit for skip behavior.
examples/run-examples.py Runs examples with the dev dependency group so optional SDKs are available.
examples/check-examples.py Runs mypy for examples with the dev dependency group so optional SDKs are available.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +13 to +14
from . import _sdk, errors, protocol
from . import tools as tools_module
Comment on lines +13 to +14
from . import _sdk, errors, protocol
from . import tools as tools_module
The `ai` package no longer depends on `openai` and `anthropic` directly
and instead loads the SDKs lazily when a corresponding provider is
instantiated.  Upstream SDKs can be pulled-in via appropriately named
optional dependency groups.
@elprans elprans force-pushed the elprans/optional-sdks branch from 86d91d1 to b1fba0f Compare May 14, 2026 21:58
@elprans elprans merged commit 8e7601f into main May 14, 2026
5 checks passed
@elprans elprans deleted the elprans/optional-sdks branch May 14, 2026 22:11
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