Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.15.2"
".": "0.16.0"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.16.0 (2026-01-30)

Full Changelog: [v0.15.2...v0.16.0](https://github.com/openlayer-ai/openlayer-python/compare/v0.15.2...v0.16.0)

### Features

* **client:** add custom JSON encoder for extended type support ([b47689c](https://github.com/openlayer-ai/openlayer-python/commit/b47689c0a19a484ef279881e012131be2836e054))

## 0.15.2 (2026-01-26)

Full Changelog: [v0.15.1...v0.15.2](https://github.com/openlayer-ai/openlayer-python/compare/v0.15.1...v0.15.2)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openlayer"
version = "0.15.2"
version = "0.16.0"
description = "The official Python library for the openlayer API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
7 changes: 5 additions & 2 deletions src/openlayer/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
APIConnectionError,
APIResponseValidationError,
)
from ._utils._json import openapi_dumps

log: logging.Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -554,8 +555,10 @@ def _build_request(
kwargs["content"] = options.content
elif isinstance(json_data, bytes):
kwargs["content"] = json_data
else:
kwargs["json"] = json_data if is_given(json_data) else None
elif not files:
# Don't set content when JSON is sent as multipart/form-data,
# since httpx's content param overrides other body arguments
kwargs["content"] = openapi_dumps(json_data) if is_given(json_data) and json_data is not None else None
kwargs["files"] = files
else:
headers.pop("Content-Type", None)
Expand Down
6 changes: 3 additions & 3 deletions src/openlayer/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def model_dump(
exclude_defaults: bool = False,
warnings: bool = True,
mode: Literal["json", "python"] = "python",
by_alias: bool | None = None,
) -> dict[str, Any]:
if (not PYDANTIC_V1) or hasattr(model, "model_dump"):
return model.model_dump(
Expand All @@ -148,13 +149,12 @@ def model_dump(
exclude_defaults=exclude_defaults,
# warnings are not supported in Pydantic v1
warnings=True if PYDANTIC_V1 else warnings,
by_alias=by_alias,
)
return cast(
"dict[str, Any]",
model.dict( # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
exclude=exclude,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude=exclude, exclude_unset=exclude_unset, exclude_defaults=exclude_defaults, by_alias=bool(by_alias)
),
)

Expand Down
35 changes: 35 additions & 0 deletions src/openlayer/_utils/_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
from typing import Any
from datetime import datetime
from typing_extensions import override

import pydantic

from .._compat import model_dump


def openapi_dumps(obj: Any) -> bytes:
"""
Serialize an object to UTF-8 encoded JSON bytes.

Extends the standard json.dumps with support for additional types
commonly used in the SDK, such as `datetime`, `pydantic.BaseModel`, etc.
"""
return json.dumps(
obj,
cls=_CustomEncoder,
# Uses the same defaults as httpx's JSON serialization
ensure_ascii=False,
separators=(",", ":"),
allow_nan=False,
).encode()


class _CustomEncoder(json.JSONEncoder):
@override
def default(self, o: Any) -> Any:
if isinstance(o, datetime):
return o.isoformat()
if isinstance(o, pydantic.BaseModel):
return model_dump(o, exclude_unset=True, mode="json", by_alias=True)
return super().default(o)
2 changes: 1 addition & 1 deletion src/openlayer/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "openlayer"
__version__ = "0.15.2" # x-release-please-version
__version__ = "0.16.0" # x-release-please-version
126 changes: 126 additions & 0 deletions tests/test_utils/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from __future__ import annotations

import datetime
from typing import Union

import pydantic

from openlayer import _compat
from openlayer._utils._json import openapi_dumps


class TestOpenapiDumps:
def test_basic(self) -> None:
data = {"key": "value", "number": 42}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"key":"value","number":42}'

def test_datetime_serialization(self) -> None:
dt = datetime.datetime(2023, 1, 1, 12, 0, 0)
data = {"datetime": dt}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"datetime":"2023-01-01T12:00:00"}'

def test_pydantic_model_serialization(self) -> None:
class User(pydantic.BaseModel):
first_name: str
last_name: str
age: int

model_instance = User(first_name="John", last_name="Kramer", age=83)
data = {"model": model_instance}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"first_name":"John","last_name":"Kramer","age":83}}'

def test_pydantic_model_with_default_values(self) -> None:
class User(pydantic.BaseModel):
name: str
role: str = "user"
active: bool = True
score: int = 0

model_instance = User(name="Alice")
data = {"model": model_instance}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"name":"Alice"}}'

def test_pydantic_model_with_default_values_overridden(self) -> None:
class User(pydantic.BaseModel):
name: str
role: str = "user"
active: bool = True

model_instance = User(name="Bob", role="admin", active=False)
data = {"model": model_instance}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"name":"Bob","role":"admin","active":false}}'

def test_pydantic_model_with_alias(self) -> None:
class User(pydantic.BaseModel):
first_name: str = pydantic.Field(alias="firstName")
last_name: str = pydantic.Field(alias="lastName")

model_instance = User(firstName="John", lastName="Doe")
data = {"model": model_instance}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"firstName":"John","lastName":"Doe"}}'

def test_pydantic_model_with_alias_and_default(self) -> None:
class User(pydantic.BaseModel):
user_name: str = pydantic.Field(alias="userName")
user_role: str = pydantic.Field(default="member", alias="userRole")
is_active: bool = pydantic.Field(default=True, alias="isActive")

model_instance = User(userName="charlie")
data = {"model": model_instance}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"userName":"charlie"}}'

model_with_overrides = User(userName="diana", userRole="admin", isActive=False)
data = {"model": model_with_overrides}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"userName":"diana","userRole":"admin","isActive":false}}'

def test_pydantic_model_with_nested_models_and_defaults(self) -> None:
class Address(pydantic.BaseModel):
street: str
city: str = "Unknown"

class User(pydantic.BaseModel):
name: str
address: Address
verified: bool = False

if _compat.PYDANTIC_V1:
# to handle forward references in Pydantic v1
User.update_forward_refs(**locals()) # type: ignore[reportDeprecated]

address = Address(street="123 Main St")
user = User(name="Diana", address=address)
data = {"user": user}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"user":{"name":"Diana","address":{"street":"123 Main St"}}}'

address_with_city = Address(street="456 Oak Ave", city="Boston")
user_verified = User(name="Eve", address=address_with_city, verified=True)
data = {"user": user_verified}
json_bytes = openapi_dumps(data)
assert (
json_bytes == b'{"user":{"name":"Eve","address":{"street":"456 Oak Ave","city":"Boston"},"verified":true}}'
)

def test_pydantic_model_with_optional_fields(self) -> None:
class User(pydantic.BaseModel):
name: str
email: Union[str, None]
phone: Union[str, None]

model_with_none = User(name="Eve", email=None, phone=None)
data = {"model": model_with_none}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"name":"Eve","email":null,"phone":null}}'

model_with_values = User(name="Frank", email="frank@example.com", phone=None)
data = {"model": model_with_values}
json_bytes = openapi_dumps(data)
assert json_bytes == b'{"model":{"name":"Frank","email":"frank@example.com","phone":null}}'