-
Notifications
You must be signed in to change notification settings - Fork 1
Python SDK 3.1.2: lazy imports, attribution header, SSE forward-compat #45
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -91,4 +91,35 @@ The `extraction` parameter replaces the deprecated `livecrawl` / | |||||||
| Unknown keys inside `extraction` raise `ValidationError` locally, and passing | ||||||||
| `extraction` together with `livecrawl` / `livecrawl_formats` raises | ||||||||
| `ValueError` — both mirror the server's 422 contract so callers fail-fast. | ||||||||
| <!-- End SDK Example Usage [extraction] --> | ||||||||
| <!-- End SDK Example Usage [extraction] --> | ||||||||
|
|
||||||||
| <!-- Start SDK Example Usage [attribution] --> | ||||||||
| ```python | ||||||||
| # Tag every outbound request with a caller-identity header so the | ||||||||
| # analytics layer can split SDK traffic from MCP traffic. | ||||||||
| import os | ||||||||
| from youdotcom import You | ||||||||
|
|
||||||||
|
|
||||||||
| with You( | ||||||||
| api_key_auth=os.getenv("YDC_API_KEY"), | ||||||||
| app_title="MyAgent", | ||||||||
| app_url="https://example.com", | ||||||||
| timeout_ms=60_000, | ||||||||
| ) as you: | ||||||||
|
|
||||||||
| res = you.search(query="What did OpenAI announce this week?") | ||||||||
|
|
||||||||
| # Handle response | ||||||||
| print(res) | ||||||||
| ``` | ||||||||
|
|
||||||||
| `X-Client-Info` sent on the wire: | ||||||||
|
|
||||||||
| ``` | ||||||||
| python-sdk; client=youdotcom/<version>; title=MyAgent; url=https://example.com; ua=python/<V> httpx/<V> | ||||||||
| ``` | ||||||||
|
|
||||||||
| `app_title` and `app_url` are optional. When omitted, those segments are | ||||||||
| dropped entirely. | ||||||||
| <!-- End SDK Example Usage [attribution] --> | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Add a trailing newline to USAGE.md USAGE.md still ends without a final newline (shown in the diff as
Suggested change
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,99 @@ | ||
| """Public surface for ``youdotcom``. | ||
|
|
||
| Imports are resolved lazily via PEP 562 module ``__getattr__`` so that | ||
| ``import youdotcom`` does **not** pull transport-layer modules | ||
| (``httpx``, ``urllib.request``) into ``sys.modules``. This matters for | ||
| Temporal Workflow sandboxes, which reject transport imports at Worker | ||
| construction time and cannot be patched around with | ||
| ``workflow.unsafe.imports_passed_through()`` because the parent package | ||
| import runs before any submodule body. | ||
|
|
||
| Public surface (trying out ``from youdotcom import <name>``): | ||
|
|
||
| - ``You`` — the unified API client (from ``.sdk``) | ||
| - ``VERSION`` / ``OPENAPI_DOC_VERSION`` / ``USER_AGENT`` — version pins | ||
| populated from ``_version.py`` at module load | ||
|
|
||
| Sub-packages accessed as ``youdotcom.<name>.X``: | ||
|
|
||
| - ``models``, ``errors``, ``utils``, ``types``, ``_hooks``, ``_shims`` | ||
|
|
||
| Lazy-init port. Mirrors the pattern used in | ||
| ``youdotcom.models.__init__`` (shipped in 3.0.0) at the SDK root. | ||
| """ | ||
|
|
||
| from typing import Any, TYPE_CHECKING | ||
|
|
||
| from youdotcom.utils.dynamic_imports import lazy_getattr, lazy_dir | ||
|
|
||
| from ._version import ( | ||
| __title__, | ||
| __version__, | ||
| __openapi_doc_version__, | ||
| __title__, | ||
| __user_agent__, | ||
| __version__, | ||
| ) | ||
| from .sdk import * | ||
| from .sdkconfiguration import * | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .sdk import You | ||
|
|
||
|
|
||
| __all__ = [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] Reconcile the root import surface with the "no breaking changes" claim Previously |
||
| "OPENAPI_DOC_VERSION", | ||
| "USER_AGENT", | ||
| "VERSION", | ||
| "You", | ||
| "__openapi_doc_version__", | ||
| "__title__", | ||
| "__user_agent__", | ||
| "__version__", | ||
| ] | ||
|
|
||
|
|
||
| # Explicit module-level constants. These are cheap strings resolved | ||
| # eagerly from ``_version.py``, which doesn't pull transport-layer | ||
| # modules. Keeping them as real attributes (vs. routing through | ||
| # ``__getattr__``) preserves `from youdotcom import VERSION` ergonomics | ||
| # and avoids the overhead of an indirection on a one-line lookup. | ||
| VERSION: str = __version__ | ||
| OPENAPI_DOC_VERSION = __openapi_doc_version__ | ||
| USER_AGENT = __user_agent__ | ||
| OPENAPI_DOC_VERSION: str = __openapi_doc_version__ | ||
| USER_AGENT: str = __user_agent__ | ||
|
|
||
|
|
||
| # Lazy mapping for the single non-constant public attribute, ``You``. | ||
| _dynamic_imports: dict[str, str] = { | ||
| "You": ".sdk", | ||
| } | ||
|
|
||
|
|
||
| # Sub-packages accessible as ``youdotcom.<name>`` (PEP 562 routes the | ||
| # attribute lookup through ``__getattr__`` so the submodule is imported | ||
| # on demand, the first time someone touches it). | ||
| _sub_packages: list[str] = [ | ||
| "_hooks", | ||
| "_shims", | ||
| "errors", | ||
| "models", | ||
| "types", | ||
| "utils", | ||
| ] | ||
|
|
||
|
|
||
| def __getattr__(attr_name: str) -> Any: | ||
| return lazy_getattr( | ||
| attr_name, | ||
| package=__package__, | ||
| dynamic_imports=_dynamic_imports, | ||
| sub_packages=_sub_packages, | ||
| ) | ||
|
|
||
|
|
||
| def __dir__(): | ||
| return sorted( | ||
| set( | ||
| lazy_dir( | ||
| dynamic_imports=_dynamic_imports, | ||
| sub_packages=_sub_packages, | ||
| ) | ||
| ) | ||
| | set(__all__) | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Align CHANGELOG
import *claim with actual exportsThe CHANGELOG says
from youdotcom import *is narrowed to (You,VERSION,OPENAPI_DOC_VERSION,USER_AGENT), butsrc/youdotcom/__init__.py's__all__also exports version metadata dunders like__version__,__user_agent__,__openapi_doc_version__, and__title__. Either update the CHANGELOG wording to mention the dunder exports, or trim__all__so the release notes match the actual wildcard-import surface.