Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions src/landingai_ade/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,39 @@ async def get_next_page(self: AsyncPageT) -> AsyncPageT:
_DefaultStreamT = TypeVar("_DefaultStreamT", bound=Union[Stream[Any], AsyncStream[Any]])


def _contains_binary(value: object) -> bool:
"""Check whether `value` contains a `bytes`/`bytearray` at any depth."""
if isinstance(value, (bytes, bytearray)):
return True
if isinstance(value, (tuple, list)):
return any(_contains_binary(item) for item in cast("tuple[object, ...] | list[object]", value))
if isinstance(value, dict):
return any(_contains_binary(item) for item in cast("dict[object, object]", value).values())
return False


def _redact_binary_for_logging(value: object) -> object:
"""Replace raw binary payloads (e.g. uploaded file contents) with a short
placeholder so debug logs don't dump megabytes of unreadable bytes.

Returns `value` unchanged when it contains no binary data, so the common
case (no bytes anywhere) allocates nothing new.
"""
if not _contains_binary(value):
return value
if isinstance(value, (bytes, bytearray)):
return f"<{len(value)} bytes>"
if isinstance(value, tuple):
return tuple(_redact_binary_for_logging(item) for item in cast("tuple[object, ...]", value))
if isinstance(value, list):
return [_redact_binary_for_logging(item) for item in cast("list[object]", value)]
if isinstance(value, dict):
return {
key: _redact_binary_for_logging(item) for key, item in cast("dict[object, object]", value).items()
}
return value


class BaseClient(Generic[_HttpxClientT, _DefaultStreamT]):
_client: _HttpxClientT
_version: str
Expand Down Expand Up @@ -484,15 +517,17 @@ def _build_request(
if log.isEnabledFor(logging.DEBUG):
log.debug(
"Request options: %s",
model_dump(
options,
exclude_unset=True,
# Pydantic v1 can't dump every type we support in content, so we exclude it for now.
exclude={
"content",
}
if PYDANTIC_V1
else {},
_redact_binary_for_logging(
model_dump(
Comment on lines +520 to +521
options,
exclude_unset=True,
# Pydantic v1 can't dump every type we support in content, so we exclude it for now.
exclude={
"content",
}
if PYDANTIC_V1
else {},
)
),
)
kwargs: dict[str, Any] = {}
Expand Down