diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index e5caf73..cb10d13 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,12 +1,14 @@
name: CI
on:
push:
- branches-ignore:
- - 'generated'
- - 'codegen/**'
- - 'integrated/**'
- - 'stl-preview-head/**'
- - 'stl-preview-base/**'
+ branches:
+ - '**'
+ - '!integrated/**'
+ - '!stl-preview-head/**'
+ - '!stl-preview-base/**'
+ - '!generated'
+ - '!codegen/**'
+ - 'codegen/stl/**'
pull_request:
branches-ignore:
- 'stl-preview-head/**'
@@ -17,7 +19,7 @@ jobs:
timeout-minutes: 10
name: lint
runs-on: ${{ github.repository == 'stainless-sdks/unlayer-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
- if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
+ if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')
steps:
- uses: actions/checkout@v6
@@ -36,7 +38,7 @@ jobs:
run: ./scripts/lint
build:
- if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
+ if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')
timeout-minutes: 10
name: build
permissions:
@@ -61,14 +63,18 @@ jobs:
run: rye build
- name: Get GitHub OIDC Token
- if: github.repository == 'stainless-sdks/unlayer-python'
+ if: |-
+ github.repository == 'stainless-sdks/unlayer-python' &&
+ !startsWith(github.ref, 'refs/heads/stl/')
id: github-oidc
uses: actions/github-script@v8
with:
script: core.setOutput('github_token', await core.getIDToken());
- name: Upload tarball
- if: github.repository == 'stainless-sdks/unlayer-python'
+ if: |-
+ github.repository == 'stainless-sdks/unlayer-python' &&
+ !startsWith(github.ref, 'refs/heads/stl/')
env:
URL: https://pkg.stainless.com/s
AUTH: ${{ steps.github-oidc.outputs.github_token }}
diff --git a/.gitignore b/.gitignore
index 95ceb18..3824f4c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
.prism.log
+.stdy.log
_dev
__pycache__
diff --git a/.release-please-manifest.json b/.release-please-manifest.json
index 3d2ac0b..10f3091 100644
--- a/.release-please-manifest.json
+++ b/.release-please-manifest.json
@@ -1,3 +1,3 @@
{
- ".": "0.1.0"
+ ".": "0.2.0"
}
\ No newline at end of file
diff --git a/.stats.yml b/.stats.yml
index 2702d73..2750c56 100644
--- a/.stats.yml
+++ b/.stats.yml
@@ -1,4 +1,4 @@
-configured_endpoints: 7
-openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/unlayer%2Funlayer-48f00d1c04c23fb4d1cb7cf4af4f56b0c920d758c1f06e06e5373e5b15e9c27d.yml
-openapi_spec_hash: 6ee2a94bb9840aceb4a6161c724ce46c
-config_hash: 249869757b6eb98ae3d58f2a47ce21e2
+configured_endpoints: 8
+openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/unlayer%2Funlayer-a1351fe18005248184e11e2c1d6e4a8df7ecfd0092f9fcfeabaa025a2c5b4986.yml
+openapi_spec_hash: 3633a7fdec0e4c3d72dcbadeebaea907
+config_hash: 6f1858ca62cea01f7c1c4427b9263c25
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3935a47..eba4386 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,53 @@
# Changelog
+## 0.2.0 (2026-04-23)
+
+Full Changelog: [v0.1.0...v0.2.0](https://github.com/unlayer/unlayer-python/compare/v0.1.0...v0.2.0)
+
+### Features
+
+* **api:** api update ([d8c02a2](https://github.com/unlayer/unlayer-python/commit/d8c02a28202f28aff216e1c51b4dc183bf758246))
+* **internal:** implement indices array format for query and form serialization ([e8555c7](https://github.com/unlayer/unlayer-python/commit/e8555c785eb5ac3fd4f46eba86514c83279c8455))
+
+
+### Bug Fixes
+
+* **client:** preserve hardcoded query params when merging with user params ([75acb50](https://github.com/unlayer/unlayer-python/commit/75acb50baa973b93439b0e1e803e34ec5ea0f891))
+* **deps:** bump minimum typing-extensions version ([b7baa1e](https://github.com/unlayer/unlayer-python/commit/b7baa1e5d138b5a33b63bed9dae37bfabb6d968d))
+* ensure file data are only sent as 1 parameter ([16c4a95](https://github.com/unlayer/unlayer-python/commit/16c4a958a038f89843bfd0287656f14c222bbc9a))
+* **pydantic:** do not pass `by_alias` unless set ([d31637b](https://github.com/unlayer/unlayer-python/commit/d31637bd182be359df4af936cb715cc554d9a14b))
+* sanitize endpoint path params ([bc0d6b6](https://github.com/unlayer/unlayer-python/commit/bc0d6b6b147d33cd2f65855284e7f435bdee09f9))
+
+
+### Performance Improvements
+
+* **client:** optimize file structure copying in multipart requests ([9e8bba9](https://github.com/unlayer/unlayer-python/commit/9e8bba9900b5252766dae4d7cd835fb264136ed4))
+
+
+### Chores
+
+* **ci:** skip lint on metadata-only changes ([3343492](https://github.com/unlayer/unlayer-python/commit/33434927ad6ab7c508d758335944cdc4123a5e17))
+* **ci:** skip uploading artifacts on stainless-internal branches ([907041a](https://github.com/unlayer/unlayer-python/commit/907041a9070f3c4a86605a21c9ab9a004a9bf553))
+* **internal:** codegen related update ([d58e1b8](https://github.com/unlayer/unlayer-python/commit/d58e1b80f026a3fbb93c51e57af8afedcdb866e9))
+* **internal:** make `test_proxy_environment_variables` more resilient to env ([caea803](https://github.com/unlayer/unlayer-python/commit/caea803e4fc873ade6fbc9b9b66ff67938780f65))
+* **internal:** more robust bootstrap script ([2543d0b](https://github.com/unlayer/unlayer-python/commit/2543d0b9a29711c39f5b8b5bfeac15dfc66b79b3))
+* **internal:** tweak CI branches ([4e152dc](https://github.com/unlayer/unlayer-python/commit/4e152dccd0191c2dd4d321e8ca7b6550dc6bc3f0))
+* **internal:** update gitignore ([5a510e6](https://github.com/unlayer/unlayer-python/commit/5a510e6edf2e26d612be067b6c95e70780d01d57))
+* **test:** do not count install time for mock server timeout ([3ad7bbb](https://github.com/unlayer/unlayer-python/commit/3ad7bbb203d27a7b7f69fda2d0f3aebea329c49e))
+* **tests:** bump steady to v0.19.4 ([775fe46](https://github.com/unlayer/unlayer-python/commit/775fe461f02f96ebc4b7cd6ff2d8cab4c52d291d))
+* **tests:** bump steady to v0.19.5 ([9ef3615](https://github.com/unlayer/unlayer-python/commit/9ef3615041ea171a462a29f8cfd268133df0c6fc))
+* **tests:** bump steady to v0.19.6 ([33d721e](https://github.com/unlayer/unlayer-python/commit/33d721e46826c23bf5b1ce78477d003cb9cd83da))
+* **tests:** bump steady to v0.19.7 ([3d09621](https://github.com/unlayer/unlayer-python/commit/3d09621f6d61c74d431b9af1336c6c51d8396d4f))
+* **tests:** bump steady to v0.20.1 ([649317d](https://github.com/unlayer/unlayer-python/commit/649317dda33455a5b5e25aedeb0210a746b767f2))
+* **tests:** bump steady to v0.20.2 ([2c51cba](https://github.com/unlayer/unlayer-python/commit/2c51cba746385da9a9e5ec0ea00bf6c4cdee2bbd))
+* **tests:** bump steady to v0.22.1 ([0a6f60a](https://github.com/unlayer/unlayer-python/commit/0a6f60a8acb8c85a937166884ba523cc59821b8f))
+
+
+### Refactors
+
+* **tests:** switch from prism to steady ([d2618bd](https://github.com/unlayer/unlayer-python/commit/d2618bd109fcd053b5845009bc8221090930a369))
+* **types:** use `extra_items` from PEP 728 ([19677f4](https://github.com/unlayer/unlayer-python/commit/19677f4a04cd989500eecce93cbb0506cf1ebf32))
+
## 0.1.0 (2026-02-24)
Full Changelog: [v0.0.1...v0.1.0](https://github.com/unlayer/unlayer-python/compare/v0.0.1...v0.1.0)
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8f8a253..38563d9 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -85,7 +85,7 @@ $ pip install ./path-to-wheel-file.whl
## Running tests
-Most tests require you to [set up a mock server](https://github.com/stoplightio/prism) against the OpenAPI spec to run the tests.
+Most tests require you to [set up a mock server](https://github.com/dgellow/steady) against the OpenAPI spec to run the tests.
```sh
$ ./scripts/mock
diff --git a/README.md b/README.md
index f4c8537..77b79ae 100644
--- a/README.md
+++ b/README.md
@@ -199,10 +199,15 @@ from unlayer import Unlayer
client = Unlayer()
-full_to_simple = client.convert.full_to_simple.create(
- design={"body": {"foo": "bar"}},
+generate = client.ai.generate.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
)
-print(full_to_simple.design)
+print(generate.output)
```
## Handling errors
diff --git a/api.md b/api.md
index 4166417..3c78314 100644
--- a/api.md
+++ b/api.md
@@ -1,3 +1,17 @@
+# AI
+
+## Generate
+
+Types:
+
+```python
+from unlayer.types.ai import GenerateCreateResponse
+```
+
+Methods:
+
+- client.ai.generate.create(\*\*params) -> GenerateCreateResponse
+
# Convert
## FullToSimple
diff --git a/pyproject.toml b/pyproject.toml
index cbb8b33..1bff565 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "unlayer"
-version = "0.1.0"
+version = "0.2.0"
description = "The official Python library for the unlayer API"
dynamic = ["readme"]
license = "Apache-2.0"
@@ -11,7 +11,7 @@ authors = [
dependencies = [
"httpx>=0.23.0, <1",
"pydantic>=1.9.0, <3",
- "typing-extensions>=4.10, <5",
+ "typing-extensions>=4.14, <5",
"anyio>=3.5.0, <5",
"distro>=1.7.0, <2",
"sniffio",
diff --git a/scripts/bootstrap b/scripts/bootstrap
index b430fee..fe8451e 100755
--- a/scripts/bootstrap
+++ b/scripts/bootstrap
@@ -4,7 +4,7 @@ set -e
cd "$(dirname "$0")/.."
-if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "$SKIP_BREW" != "1" ] && [ -t 0 ]; then
+if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "${SKIP_BREW:-}" != "1" ] && [ -t 0 ]; then
brew bundle check >/dev/null 2>&1 || {
echo -n "==> Install Homebrew dependencies? (y/N): "
read -r response
diff --git a/scripts/mock b/scripts/mock
index 0b28f6e..feebe5e 100755
--- a/scripts/mock
+++ b/scripts/mock
@@ -19,23 +19,34 @@ fi
echo "==> Starting mock server with URL ${URL}"
-# Run prism mock on the given spec
+# Run steady mock on the given spec
if [ "$1" == "--daemon" ]; then
- npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock "$URL" &> .prism.log &
+ # Pre-install the package so the download doesn't eat into the startup timeout
+ npm exec --package=@stdy/cli@0.22.1 -- steady --version
- # Wait for server to come online
+ npm exec --package=@stdy/cli@0.22.1 -- steady --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets "$URL" &> .stdy.log &
+
+ # Wait for server to come online via health endpoint (max 30s)
echo -n "Waiting for server"
- while ! grep -q "✖ fatal\|Prism is listening" ".prism.log" ; do
+ attempts=0
+ while ! curl --silent --fail "http://127.0.0.1:4010/_x-steady/health" >/dev/null 2>&1; do
+ if ! kill -0 $! 2>/dev/null; then
+ echo
+ cat .stdy.log
+ exit 1
+ fi
+ attempts=$((attempts + 1))
+ if [ "$attempts" -ge 300 ]; then
+ echo
+ echo "Timed out waiting for Steady server to start"
+ cat .stdy.log
+ exit 1
+ fi
echo -n "."
sleep 0.1
done
- if grep -q "✖ fatal" ".prism.log"; then
- cat .prism.log
- exit 1
- fi
-
echo
else
- npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock "$URL"
+ npm exec --package=@stdy/cli@0.22.1 -- steady --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets "$URL"
fi
diff --git a/scripts/test b/scripts/test
index dbeda2d..19acc91 100755
--- a/scripts/test
+++ b/scripts/test
@@ -9,8 +9,8 @@ GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
-function prism_is_running() {
- curl --silent "http://localhost:4010" >/dev/null 2>&1
+function steady_is_running() {
+ curl --silent "http://127.0.0.1:4010/_x-steady/health" >/dev/null 2>&1
}
kill_server_on_port() {
@@ -25,7 +25,7 @@ function is_overriding_api_base_url() {
[ -n "$TEST_API_BASE_URL" ]
}
-if ! is_overriding_api_base_url && ! prism_is_running ; then
+if ! is_overriding_api_base_url && ! steady_is_running ; then
# When we exit this script, make sure to kill the background mock server process
trap 'kill_server_on_port 4010' EXIT
@@ -36,19 +36,19 @@ fi
if is_overriding_api_base_url ; then
echo -e "${GREEN}✔ Running tests against ${TEST_API_BASE_URL}${NC}"
echo
-elif ! prism_is_running ; then
- echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Prism server"
+elif ! steady_is_running ; then
+ echo -e "${RED}ERROR:${NC} The test suite will not run without a mock Steady server"
echo -e "running against your OpenAPI spec."
echo
echo -e "To run the server, pass in the path or url of your OpenAPI"
- echo -e "spec to the prism command:"
+ echo -e "spec to the steady command:"
echo
- echo -e " \$ ${YELLOW}npm exec --package=@stainless-api/prism-cli@5.15.0 -- prism mock path/to/your.openapi.yml${NC}"
+ echo -e " \$ ${YELLOW}npm exec --package=@stdy/cli@0.22.1 -- steady path/to/your.openapi.yml --host 127.0.0.1 -p 4010 --validator-query-array-format=comma --validator-form-array-format=comma --validator-query-object-format=brackets --validator-form-object-format=brackets${NC}"
echo
exit 1
else
- echo -e "${GREEN}✔ Mock prism server is running with your OpenAPI spec${NC}"
+ echo -e "${GREEN}✔ Mock steady server is running with your OpenAPI spec${NC}"
echo
fi
diff --git a/src/unlayer/_base_client.py b/src/unlayer/_base_client.py
index c199ad7..21b1255 100644
--- a/src/unlayer/_base_client.py
+++ b/src/unlayer/_base_client.py
@@ -540,6 +540,10 @@ def _build_request(
files = cast(HttpxRequestFiles, ForceMultipartDict())
prepared_url = self._prepare_url(options.url)
+ # preserve hard-coded query params from the url
+ if params and prepared_url.query:
+ params = {**dict(prepared_url.params.items()), **params}
+ prepared_url = prepared_url.copy_with(raw_path=prepared_url.raw_path.split(b"?", 1)[0])
if "_" in prepared_url.host:
# work around https://github.com/encode/httpx/discussions/2880
kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")}
diff --git a/src/unlayer/_client.py b/src/unlayer/_client.py
index 08f7fef..09dd3fd 100644
--- a/src/unlayer/_client.py
+++ b/src/unlayer/_client.py
@@ -32,7 +32,8 @@
)
if TYPE_CHECKING:
- from .resources import convert, projects, templates, workspaces
+ from .resources import ai, convert, projects, templates, workspaces
+ from .resources.ai.ai import AIResource, AsyncAIResource
from .resources.projects import ProjectsResource, AsyncProjectsResource
from .resources.templates import TemplatesResource, AsyncTemplatesResource
from .resources.workspaces import WorkspacesResource, AsyncWorkspacesResource
@@ -107,6 +108,12 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)
+ @cached_property
+ def ai(self) -> AIResource:
+ from .resources.ai import AIResource
+
+ return AIResource(self)
+
@cached_property
def convert(self) -> ConvertResource:
from .resources.convert import ConvertResource
@@ -115,18 +122,21 @@ def convert(self) -> ConvertResource:
@cached_property
def projects(self) -> ProjectsResource:
+ """Project details and configuration."""
from .resources.projects import ProjectsResource
return ProjectsResource(self)
@cached_property
def templates(self) -> TemplatesResource:
+ """Template management and retrieval."""
from .resources.templates import TemplatesResource
return TemplatesResource(self)
@cached_property
def workspaces(self) -> WorkspacesResource:
+ """Workspace access and management."""
from .resources.workspaces import WorkspacesResource
return WorkspacesResource(self)
@@ -337,6 +347,12 @@ def __init__(
_strict_response_validation=_strict_response_validation,
)
+ @cached_property
+ def ai(self) -> AsyncAIResource:
+ from .resources.ai import AsyncAIResource
+
+ return AsyncAIResource(self)
+
@cached_property
def convert(self) -> AsyncConvertResource:
from .resources.convert import AsyncConvertResource
@@ -345,18 +361,21 @@ def convert(self) -> AsyncConvertResource:
@cached_property
def projects(self) -> AsyncProjectsResource:
+ """Project details and configuration."""
from .resources.projects import AsyncProjectsResource
return AsyncProjectsResource(self)
@cached_property
def templates(self) -> AsyncTemplatesResource:
+ """Template management and retrieval."""
from .resources.templates import AsyncTemplatesResource
return AsyncTemplatesResource(self)
@cached_property
def workspaces(self) -> AsyncWorkspacesResource:
+ """Workspace access and management."""
from .resources.workspaces import AsyncWorkspacesResource
return AsyncWorkspacesResource(self)
@@ -507,6 +526,12 @@ class UnlayerWithRawResponse:
def __init__(self, client: Unlayer) -> None:
self._client = client
+ @cached_property
+ def ai(self) -> ai.AIResourceWithRawResponse:
+ from .resources.ai import AIResourceWithRawResponse
+
+ return AIResourceWithRawResponse(self._client.ai)
+
@cached_property
def convert(self) -> convert.ConvertResourceWithRawResponse:
from .resources.convert import ConvertResourceWithRawResponse
@@ -515,18 +540,21 @@ def convert(self) -> convert.ConvertResourceWithRawResponse:
@cached_property
def projects(self) -> projects.ProjectsResourceWithRawResponse:
+ """Project details and configuration."""
from .resources.projects import ProjectsResourceWithRawResponse
return ProjectsResourceWithRawResponse(self._client.projects)
@cached_property
def templates(self) -> templates.TemplatesResourceWithRawResponse:
+ """Template management and retrieval."""
from .resources.templates import TemplatesResourceWithRawResponse
return TemplatesResourceWithRawResponse(self._client.templates)
@cached_property
def workspaces(self) -> workspaces.WorkspacesResourceWithRawResponse:
+ """Workspace access and management."""
from .resources.workspaces import WorkspacesResourceWithRawResponse
return WorkspacesResourceWithRawResponse(self._client.workspaces)
@@ -538,6 +566,12 @@ class AsyncUnlayerWithRawResponse:
def __init__(self, client: AsyncUnlayer) -> None:
self._client = client
+ @cached_property
+ def ai(self) -> ai.AsyncAIResourceWithRawResponse:
+ from .resources.ai import AsyncAIResourceWithRawResponse
+
+ return AsyncAIResourceWithRawResponse(self._client.ai)
+
@cached_property
def convert(self) -> convert.AsyncConvertResourceWithRawResponse:
from .resources.convert import AsyncConvertResourceWithRawResponse
@@ -546,18 +580,21 @@ def convert(self) -> convert.AsyncConvertResourceWithRawResponse:
@cached_property
def projects(self) -> projects.AsyncProjectsResourceWithRawResponse:
+ """Project details and configuration."""
from .resources.projects import AsyncProjectsResourceWithRawResponse
return AsyncProjectsResourceWithRawResponse(self._client.projects)
@cached_property
def templates(self) -> templates.AsyncTemplatesResourceWithRawResponse:
+ """Template management and retrieval."""
from .resources.templates import AsyncTemplatesResourceWithRawResponse
return AsyncTemplatesResourceWithRawResponse(self._client.templates)
@cached_property
def workspaces(self) -> workspaces.AsyncWorkspacesResourceWithRawResponse:
+ """Workspace access and management."""
from .resources.workspaces import AsyncWorkspacesResourceWithRawResponse
return AsyncWorkspacesResourceWithRawResponse(self._client.workspaces)
@@ -569,6 +606,12 @@ class UnlayerWithStreamedResponse:
def __init__(self, client: Unlayer) -> None:
self._client = client
+ @cached_property
+ def ai(self) -> ai.AIResourceWithStreamingResponse:
+ from .resources.ai import AIResourceWithStreamingResponse
+
+ return AIResourceWithStreamingResponse(self._client.ai)
+
@cached_property
def convert(self) -> convert.ConvertResourceWithStreamingResponse:
from .resources.convert import ConvertResourceWithStreamingResponse
@@ -577,18 +620,21 @@ def convert(self) -> convert.ConvertResourceWithStreamingResponse:
@cached_property
def projects(self) -> projects.ProjectsResourceWithStreamingResponse:
+ """Project details and configuration."""
from .resources.projects import ProjectsResourceWithStreamingResponse
return ProjectsResourceWithStreamingResponse(self._client.projects)
@cached_property
def templates(self) -> templates.TemplatesResourceWithStreamingResponse:
+ """Template management and retrieval."""
from .resources.templates import TemplatesResourceWithStreamingResponse
return TemplatesResourceWithStreamingResponse(self._client.templates)
@cached_property
def workspaces(self) -> workspaces.WorkspacesResourceWithStreamingResponse:
+ """Workspace access and management."""
from .resources.workspaces import WorkspacesResourceWithStreamingResponse
return WorkspacesResourceWithStreamingResponse(self._client.workspaces)
@@ -600,6 +646,12 @@ class AsyncUnlayerWithStreamedResponse:
def __init__(self, client: AsyncUnlayer) -> None:
self._client = client
+ @cached_property
+ def ai(self) -> ai.AsyncAIResourceWithStreamingResponse:
+ from .resources.ai import AsyncAIResourceWithStreamingResponse
+
+ return AsyncAIResourceWithStreamingResponse(self._client.ai)
+
@cached_property
def convert(self) -> convert.AsyncConvertResourceWithStreamingResponse:
from .resources.convert import AsyncConvertResourceWithStreamingResponse
@@ -608,18 +660,21 @@ def convert(self) -> convert.AsyncConvertResourceWithStreamingResponse:
@cached_property
def projects(self) -> projects.AsyncProjectsResourceWithStreamingResponse:
+ """Project details and configuration."""
from .resources.projects import AsyncProjectsResourceWithStreamingResponse
return AsyncProjectsResourceWithStreamingResponse(self._client.projects)
@cached_property
def templates(self) -> templates.AsyncTemplatesResourceWithStreamingResponse:
+ """Template management and retrieval."""
from .resources.templates import AsyncTemplatesResourceWithStreamingResponse
return AsyncTemplatesResourceWithStreamingResponse(self._client.templates)
@cached_property
def workspaces(self) -> workspaces.AsyncWorkspacesResourceWithStreamingResponse:
+ """Workspace access and management."""
from .resources.workspaces import AsyncWorkspacesResourceWithStreamingResponse
return AsyncWorkspacesResourceWithStreamingResponse(self._client.workspaces)
diff --git a/src/unlayer/_compat.py b/src/unlayer/_compat.py
index 786ff42..e6690a4 100644
--- a/src/unlayer/_compat.py
+++ b/src/unlayer/_compat.py
@@ -2,7 +2,7 @@
from typing import TYPE_CHECKING, Any, Union, Generic, TypeVar, Callable, cast, overload
from datetime import date, datetime
-from typing_extensions import Self, Literal
+from typing_extensions import Self, Literal, TypedDict
import pydantic
from pydantic.fields import FieldInfo
@@ -131,6 +131,10 @@ def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str:
return model.model_dump_json(indent=indent)
+class _ModelDumpKwargs(TypedDict, total=False):
+ by_alias: bool
+
+
def model_dump(
model: pydantic.BaseModel,
*,
@@ -142,6 +146,9 @@ def model_dump(
by_alias: bool | None = None,
) -> dict[str, Any]:
if (not PYDANTIC_V1) or hasattr(model, "model_dump"):
+ kwargs: _ModelDumpKwargs = {}
+ if by_alias is not None:
+ kwargs["by_alias"] = by_alias
return model.model_dump(
mode=mode,
exclude=exclude,
@@ -149,7 +156,7 @@ 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,
+ **kwargs,
)
return cast(
"dict[str, Any]",
diff --git a/src/unlayer/_files.py b/src/unlayer/_files.py
index cc14c14..0fdce17 100644
--- a/src/unlayer/_files.py
+++ b/src/unlayer/_files.py
@@ -3,8 +3,8 @@
import io
import os
import pathlib
-from typing import overload
-from typing_extensions import TypeGuard
+from typing import Sequence, cast, overload
+from typing_extensions import TypeVar, TypeGuard
import anyio
@@ -17,7 +17,9 @@
HttpxFileContent,
HttpxRequestFiles,
)
-from ._utils import is_tuple_t, is_mapping_t, is_sequence_t
+from ._utils import is_list, is_mapping, is_tuple_t, is_mapping_t, is_sequence_t
+
+_T = TypeVar("_T")
def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]:
@@ -121,3 +123,51 @@ async def async_read_file_content(file: FileContent) -> HttpxFileContent:
return await anyio.Path(file).read_bytes()
return file
+
+
+def deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]]) -> _T:
+ """Copy only the containers along the given paths.
+
+ Used to guard against mutation by extract_files without copying the entire structure.
+ Only dicts and lists that lie on a path are copied; everything else
+ is returned by reference.
+
+ For example, given paths=[["foo", "files", "file"]] and the structure:
+ {
+ "foo": {
+ "bar": {"baz": {}},
+ "files": {"file": }
+ }
+ }
+ The root dict, "foo", and "files" are copied (they lie on the path).
+ "bar" and "baz" are returned by reference (off the path).
+ """
+ return _deepcopy_with_paths(item, paths, 0)
+
+
+def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T:
+ if not paths:
+ return item
+ if is_mapping(item):
+ key_to_paths: dict[str, list[Sequence[str]]] = {}
+ for path in paths:
+ if index < len(path):
+ key_to_paths.setdefault(path[index], []).append(path)
+
+ # if no path continues through this mapping, it won't be mutated and copying it is redundant
+ if not key_to_paths:
+ return item
+
+ result = dict(item)
+ for key, subpaths in key_to_paths.items():
+ if key in result:
+ result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1)
+ return cast(_T, result)
+ if is_list(item):
+ array_paths = [path for path in paths if index < len(path) and path[index] == ""]
+
+ # if no path expects a list here, nothing will be mutated inside it - return by reference
+ if not array_paths:
+ return cast(_T, item)
+ return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item])
+ return item
diff --git a/src/unlayer/_qs.py b/src/unlayer/_qs.py
index ada6fd3..de8c99b 100644
--- a/src/unlayer/_qs.py
+++ b/src/unlayer/_qs.py
@@ -101,7 +101,10 @@ def _stringify_item(
items.extend(self._stringify_item(key, item, opts))
return items
elif array_format == "indices":
- raise NotImplementedError("The array indices format is not supported yet")
+ items = []
+ for i, item in enumerate(value):
+ items.extend(self._stringify_item(f"{key}[{i}]", item, opts))
+ return items
elif array_format == "brackets":
items = []
key = key + "[]"
diff --git a/src/unlayer/_utils/__init__.py b/src/unlayer/_utils/__init__.py
index dc64e29..1c090e5 100644
--- a/src/unlayer/_utils/__init__.py
+++ b/src/unlayer/_utils/__init__.py
@@ -1,3 +1,4 @@
+from ._path import path_template as path_template
from ._sync import asyncify as asyncify
from ._proxy import LazyProxy as LazyProxy
from ._utils import (
@@ -23,7 +24,6 @@
coerce_integer as coerce_integer,
file_from_path as file_from_path,
strip_not_given as strip_not_given,
- deepcopy_minimal as deepcopy_minimal,
get_async_library as get_async_library,
maybe_coerce_float as maybe_coerce_float,
get_required_header as get_required_header,
diff --git a/src/unlayer/_utils/_path.py b/src/unlayer/_utils/_path.py
new file mode 100644
index 0000000..4d6e1e4
--- /dev/null
+++ b/src/unlayer/_utils/_path.py
@@ -0,0 +1,127 @@
+from __future__ import annotations
+
+import re
+from typing import (
+ Any,
+ Mapping,
+ Callable,
+)
+from urllib.parse import quote
+
+# Matches '.' or '..' where each dot is either literal or percent-encoded (%2e / %2E).
+_DOT_SEGMENT_RE = re.compile(r"^(?:\.|%2[eE]){1,2}$")
+
+_PLACEHOLDER_RE = re.compile(r"\{(\w+)\}")
+
+
+def _quote_path_segment_part(value: str) -> str:
+ """Percent-encode `value` for use in a URI path segment.
+
+ Considers characters not in `pchar` set from RFC 3986 §3.3 to be unsafe.
+ https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
+ """
+ # quote() already treats unreserved characters (letters, digits, and -._~)
+ # as safe, so we only need to add sub-delims, ':', and '@'.
+ # Notably, unlike the default `safe` for quote(), / is unsafe and must be quoted.
+ return quote(value, safe="!$&'()*+,;=:@")
+
+
+def _quote_query_part(value: str) -> str:
+ """Percent-encode `value` for use in a URI query string.
+
+ Considers &, = and characters not in `query` set from RFC 3986 §3.4 to be unsafe.
+ https://datatracker.ietf.org/doc/html/rfc3986#section-3.4
+ """
+ return quote(value, safe="!$'()*+,;:@/?")
+
+
+def _quote_fragment_part(value: str) -> str:
+ """Percent-encode `value` for use in a URI fragment.
+
+ Considers characters not in `fragment` set from RFC 3986 §3.5 to be unsafe.
+ https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
+ """
+ return quote(value, safe="!$&'()*+,;=:@/?")
+
+
+def _interpolate(
+ template: str,
+ values: Mapping[str, Any],
+ quoter: Callable[[str], str],
+) -> str:
+ """Replace {name} placeholders in `template`, quoting each value with `quoter`.
+
+ Placeholder names are looked up in `values`.
+
+ Raises:
+ KeyError: If a placeholder is not found in `values`.
+ """
+ # re.split with a capturing group returns alternating
+ # [text, name, text, name, ..., text] elements.
+ parts = _PLACEHOLDER_RE.split(template)
+
+ for i in range(1, len(parts), 2):
+ name = parts[i]
+ if name not in values:
+ raise KeyError(f"a value for placeholder {{{name}}} was not provided")
+ val = values[name]
+ if val is None:
+ parts[i] = "null"
+ elif isinstance(val, bool):
+ parts[i] = "true" if val else "false"
+ else:
+ parts[i] = quoter(str(values[name]))
+
+ return "".join(parts)
+
+
+def path_template(template: str, /, **kwargs: Any) -> str:
+ """Interpolate {name} placeholders in `template` from keyword arguments.
+
+ Args:
+ template: The template string containing {name} placeholders.
+ **kwargs: Keyword arguments to interpolate into the template.
+
+ Returns:
+ The template with placeholders interpolated and percent-encoded.
+
+ Safe characters for percent-encoding are dependent on the URI component.
+ Placeholders in path and fragment portions are percent-encoded where the `segment`
+ and `fragment` sets from RFC 3986 respectively are considered safe.
+ Placeholders in the query portion are percent-encoded where the `query` set from
+ RFC 3986 §3.3 is considered safe except for = and & characters.
+
+ Raises:
+ KeyError: If a placeholder is not found in `kwargs`.
+ ValueError: If resulting path contains /./ or /../ segments (including percent-encoded dot-segments).
+ """
+ # Split the template into path, query, and fragment portions.
+ fragment_template: str | None = None
+ query_template: str | None = None
+
+ rest = template
+ if "#" in rest:
+ rest, fragment_template = rest.split("#", 1)
+ if "?" in rest:
+ rest, query_template = rest.split("?", 1)
+ path_template = rest
+
+ # Interpolate each portion with the appropriate quoting rules.
+ path_result = _interpolate(path_template, kwargs, _quote_path_segment_part)
+
+ # Reject dot-segments (. and ..) in the final assembled path. The check
+ # runs after interpolation so that adjacent placeholders or a mix of static
+ # text and placeholders that together form a dot-segment are caught.
+ # Also reject percent-encoded dot-segments to protect against incorrectly
+ # implemented normalization in servers/proxies.
+ for segment in path_result.split("/"):
+ if _DOT_SEGMENT_RE.match(segment):
+ raise ValueError(f"Constructed path {path_result!r} contains dot-segment {segment!r} which is not allowed")
+
+ result = path_result
+ if query_template is not None:
+ result += "?" + _interpolate(query_template, kwargs, _quote_query_part)
+ if fragment_template is not None:
+ result += "#" + _interpolate(fragment_template, kwargs, _quote_fragment_part)
+
+ return result
diff --git a/src/unlayer/_utils/_utils.py b/src/unlayer/_utils/_utils.py
index eec7f4a..771859f 100644
--- a/src/unlayer/_utils/_utils.py
+++ b/src/unlayer/_utils/_utils.py
@@ -86,8 +86,9 @@ def _extract_items(
index += 1
if is_dict(obj):
try:
- # We are at the last entry in the path so we must remove the field
- if (len(path)) == index:
+ # Remove the field if there are no more dict keys in the path,
+ # only "" traversal markers or end.
+ if all(p == "" for p in path[index:]):
item = obj.pop(key)
else:
item = obj[key]
@@ -176,21 +177,6 @@ def is_iterable(obj: object) -> TypeGuard[Iterable[object]]:
return isinstance(obj, Iterable)
-def deepcopy_minimal(item: _T) -> _T:
- """Minimal reimplementation of copy.deepcopy() that will only copy certain object types:
-
- - mappings, e.g. `dict`
- - list
-
- This is done for performance reasons.
- """
- if is_mapping(item):
- return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()})
- if is_list(item):
- return cast(_T, [deepcopy_minimal(entry) for entry in item])
- return item
-
-
# copied from https://github.com/Rapptz/RoboDanny
def human_join(seq: Sequence[str], *, delim: str = ", ", final: str = "or") -> str:
size = len(seq)
diff --git a/src/unlayer/_version.py b/src/unlayer/_version.py
index 47398ca..b0af64e 100644
--- a/src/unlayer/_version.py
+++ b/src/unlayer/_version.py
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
__title__ = "unlayer"
-__version__ = "0.1.0" # x-release-please-version
+__version__ = "0.2.0" # x-release-please-version
diff --git a/src/unlayer/resources/__init__.py b/src/unlayer/resources/__init__.py
index 1de5370..9d521b2 100644
--- a/src/unlayer/resources/__init__.py
+++ b/src/unlayer/resources/__init__.py
@@ -1,5 +1,13 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+from .ai import (
+ AIResource,
+ AsyncAIResource,
+ AIResourceWithRawResponse,
+ AsyncAIResourceWithRawResponse,
+ AIResourceWithStreamingResponse,
+ AsyncAIResourceWithStreamingResponse,
+)
from .convert import (
ConvertResource,
AsyncConvertResource,
@@ -34,6 +42,12 @@
)
__all__ = [
+ "AIResource",
+ "AsyncAIResource",
+ "AIResourceWithRawResponse",
+ "AsyncAIResourceWithRawResponse",
+ "AIResourceWithStreamingResponse",
+ "AsyncAIResourceWithStreamingResponse",
"ConvertResource",
"AsyncConvertResource",
"ConvertResourceWithRawResponse",
diff --git a/src/unlayer/resources/ai/__init__.py b/src/unlayer/resources/ai/__init__.py
new file mode 100644
index 0000000..2f677f9
--- /dev/null
+++ b/src/unlayer/resources/ai/__init__.py
@@ -0,0 +1,33 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from .ai import (
+ AIResource,
+ AsyncAIResource,
+ AIResourceWithRawResponse,
+ AsyncAIResourceWithRawResponse,
+ AIResourceWithStreamingResponse,
+ AsyncAIResourceWithStreamingResponse,
+)
+from .generate import (
+ GenerateResource,
+ AsyncGenerateResource,
+ GenerateResourceWithRawResponse,
+ AsyncGenerateResourceWithRawResponse,
+ GenerateResourceWithStreamingResponse,
+ AsyncGenerateResourceWithStreamingResponse,
+)
+
+__all__ = [
+ "GenerateResource",
+ "AsyncGenerateResource",
+ "GenerateResourceWithRawResponse",
+ "AsyncGenerateResourceWithRawResponse",
+ "GenerateResourceWithStreamingResponse",
+ "AsyncGenerateResourceWithStreamingResponse",
+ "AIResource",
+ "AsyncAIResource",
+ "AIResourceWithRawResponse",
+ "AsyncAIResourceWithRawResponse",
+ "AIResourceWithStreamingResponse",
+ "AsyncAIResourceWithStreamingResponse",
+]
diff --git a/src/unlayer/resources/ai/ai.py b/src/unlayer/resources/ai/ai.py
new file mode 100644
index 0000000..503a4f1
--- /dev/null
+++ b/src/unlayer/resources/ai/ai.py
@@ -0,0 +1,102 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+from .generate import (
+ GenerateResource,
+ AsyncGenerateResource,
+ GenerateResourceWithRawResponse,
+ AsyncGenerateResourceWithRawResponse,
+ GenerateResourceWithStreamingResponse,
+ AsyncGenerateResourceWithStreamingResponse,
+)
+from ..._compat import cached_property
+from ..._resource import SyncAPIResource, AsyncAPIResource
+
+__all__ = ["AIResource", "AsyncAIResource"]
+
+
+class AIResource(SyncAPIResource):
+ @cached_property
+ def generate(self) -> GenerateResource:
+ return GenerateResource(self._client)
+
+ @cached_property
+ def with_raw_response(self) -> AIResourceWithRawResponse:
+ """
+ This property can be used as a prefix for any HTTP method call to return
+ the raw response object instead of the parsed content.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#accessing-raw-response-data-eg-headers
+ """
+ return AIResourceWithRawResponse(self)
+
+ @cached_property
+ def with_streaming_response(self) -> AIResourceWithStreamingResponse:
+ """
+ An alternative to `.with_raw_response` that doesn't eagerly read the response body.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#with_streaming_response
+ """
+ return AIResourceWithStreamingResponse(self)
+
+
+class AsyncAIResource(AsyncAPIResource):
+ @cached_property
+ def generate(self) -> AsyncGenerateResource:
+ return AsyncGenerateResource(self._client)
+
+ @cached_property
+ def with_raw_response(self) -> AsyncAIResourceWithRawResponse:
+ """
+ This property can be used as a prefix for any HTTP method call to return
+ the raw response object instead of the parsed content.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#accessing-raw-response-data-eg-headers
+ """
+ return AsyncAIResourceWithRawResponse(self)
+
+ @cached_property
+ def with_streaming_response(self) -> AsyncAIResourceWithStreamingResponse:
+ """
+ An alternative to `.with_raw_response` that doesn't eagerly read the response body.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#with_streaming_response
+ """
+ return AsyncAIResourceWithStreamingResponse(self)
+
+
+class AIResourceWithRawResponse:
+ def __init__(self, ai: AIResource) -> None:
+ self._ai = ai
+
+ @cached_property
+ def generate(self) -> GenerateResourceWithRawResponse:
+ return GenerateResourceWithRawResponse(self._ai.generate)
+
+
+class AsyncAIResourceWithRawResponse:
+ def __init__(self, ai: AsyncAIResource) -> None:
+ self._ai = ai
+
+ @cached_property
+ def generate(self) -> AsyncGenerateResourceWithRawResponse:
+ return AsyncGenerateResourceWithRawResponse(self._ai.generate)
+
+
+class AIResourceWithStreamingResponse:
+ def __init__(self, ai: AIResource) -> None:
+ self._ai = ai
+
+ @cached_property
+ def generate(self) -> GenerateResourceWithStreamingResponse:
+ return GenerateResourceWithStreamingResponse(self._ai.generate)
+
+
+class AsyncAIResourceWithStreamingResponse:
+ def __init__(self, ai: AsyncAIResource) -> None:
+ self._ai = ai
+
+ @cached_property
+ def generate(self) -> AsyncGenerateResourceWithStreamingResponse:
+ return AsyncGenerateResourceWithStreamingResponse(self._ai.generate)
diff --git a/src/unlayer/resources/ai/generate.py b/src/unlayer/resources/ai/generate.py
new file mode 100644
index 0000000..df4acad
--- /dev/null
+++ b/src/unlayer/resources/ai/generate.py
@@ -0,0 +1,234 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+from typing import Iterable
+from typing_extensions import Literal
+
+import httpx
+
+from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
+from ..._utils import maybe_transform, async_maybe_transform
+from ..._compat import cached_property
+from ...types.ai import generate_create_params
+from ..._resource import SyncAPIResource, AsyncAPIResource
+from ..._response import (
+ to_raw_response_wrapper,
+ to_streamed_response_wrapper,
+ async_to_raw_response_wrapper,
+ async_to_streamed_response_wrapper,
+)
+from ..._base_client import make_request_options
+from ...types.ai.generate_create_response import GenerateCreateResponse
+
+__all__ = ["GenerateResource", "AsyncGenerateResource"]
+
+
+class GenerateResource(SyncAPIResource):
+ @cached_property
+ def with_raw_response(self) -> GenerateResourceWithRawResponse:
+ """
+ This property can be used as a prefix for any HTTP method call to return
+ the raw response object instead of the parsed content.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#accessing-raw-response-data-eg-headers
+ """
+ return GenerateResourceWithRawResponse(self)
+
+ @cached_property
+ def with_streaming_response(self) -> GenerateResourceWithStreamingResponse:
+ """
+ An alternative to `.with_raw_response` that doesn't eagerly read the response body.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#with_streaming_response
+ """
+ return GenerateResourceWithStreamingResponse(self)
+
+ def create(
+ self,
+ *,
+ display_mode: Literal["email", "web", "popup", "document"],
+ input: Iterable[generate_create_params.Input],
+ output: generate_create_params.Output,
+ project_id: str | Omit = omit,
+ context: generate_create_params.Context | Omit = omit,
+ model: Literal["anthropic/claude-opus-4-6", "openai/gpt-5.2"] | Omit = omit,
+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
+ # The extra values given here take precedence over values defined on the client or passed to this method.
+ extra_headers: Headers | None = None,
+ extra_query: Query | None = None,
+ extra_body: Body | None = None,
+ timeout: float | httpx.Timeout | None | NotGiven = not_given,
+ ) -> GenerateCreateResponse:
+ """Generate, modify, or import an Unlayer design using AI.
+
+ Provide typed input
+ parts to describe what to generate.
+
+ Args:
+ display_mode: Display mode for the design
+
+ input: Array of typed input parts (max 50)
+
+ output: What to generate
+
+ project_id: The project ID (required for PAT auth, auto-resolved for API key auth)
+
+ context: Editor environment context
+
+ model: AI model to use, in provider/model format. Optional — defaults to
+ anthropic/claude-opus-4-6.
+
+ extra_headers: Send extra headers
+
+ extra_query: Add additional query parameters to the request
+
+ extra_body: Add additional JSON properties to the request
+
+ timeout: Override the client-level default timeout for this request, in seconds
+ """
+ return self._post(
+ "/v3/ai/generate",
+ body=maybe_transform(
+ {
+ "display_mode": display_mode,
+ "input": input,
+ "output": output,
+ "context": context,
+ "model": model,
+ },
+ generate_create_params.GenerateCreateParams,
+ ),
+ options=make_request_options(
+ extra_headers=extra_headers,
+ extra_query=extra_query,
+ extra_body=extra_body,
+ timeout=timeout,
+ query=maybe_transform({"project_id": project_id}, generate_create_params.GenerateCreateParams),
+ ),
+ cast_to=GenerateCreateResponse,
+ )
+
+
+class AsyncGenerateResource(AsyncAPIResource):
+ @cached_property
+ def with_raw_response(self) -> AsyncGenerateResourceWithRawResponse:
+ """
+ This property can be used as a prefix for any HTTP method call to return
+ the raw response object instead of the parsed content.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#accessing-raw-response-data-eg-headers
+ """
+ return AsyncGenerateResourceWithRawResponse(self)
+
+ @cached_property
+ def with_streaming_response(self) -> AsyncGenerateResourceWithStreamingResponse:
+ """
+ An alternative to `.with_raw_response` that doesn't eagerly read the response body.
+
+ For more information, see https://www.github.com/unlayer/unlayer-python#with_streaming_response
+ """
+ return AsyncGenerateResourceWithStreamingResponse(self)
+
+ async def create(
+ self,
+ *,
+ display_mode: Literal["email", "web", "popup", "document"],
+ input: Iterable[generate_create_params.Input],
+ output: generate_create_params.Output,
+ project_id: str | Omit = omit,
+ context: generate_create_params.Context | Omit = omit,
+ model: Literal["anthropic/claude-opus-4-6", "openai/gpt-5.2"] | Omit = omit,
+ # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
+ # The extra values given here take precedence over values defined on the client or passed to this method.
+ extra_headers: Headers | None = None,
+ extra_query: Query | None = None,
+ extra_body: Body | None = None,
+ timeout: float | httpx.Timeout | None | NotGiven = not_given,
+ ) -> GenerateCreateResponse:
+ """Generate, modify, or import an Unlayer design using AI.
+
+ Provide typed input
+ parts to describe what to generate.
+
+ Args:
+ display_mode: Display mode for the design
+
+ input: Array of typed input parts (max 50)
+
+ output: What to generate
+
+ project_id: The project ID (required for PAT auth, auto-resolved for API key auth)
+
+ context: Editor environment context
+
+ model: AI model to use, in provider/model format. Optional — defaults to
+ anthropic/claude-opus-4-6.
+
+ extra_headers: Send extra headers
+
+ extra_query: Add additional query parameters to the request
+
+ extra_body: Add additional JSON properties to the request
+
+ timeout: Override the client-level default timeout for this request, in seconds
+ """
+ return await self._post(
+ "/v3/ai/generate",
+ body=await async_maybe_transform(
+ {
+ "display_mode": display_mode,
+ "input": input,
+ "output": output,
+ "context": context,
+ "model": model,
+ },
+ generate_create_params.GenerateCreateParams,
+ ),
+ options=make_request_options(
+ extra_headers=extra_headers,
+ extra_query=extra_query,
+ extra_body=extra_body,
+ timeout=timeout,
+ query=await async_maybe_transform(
+ {"project_id": project_id}, generate_create_params.GenerateCreateParams
+ ),
+ ),
+ cast_to=GenerateCreateResponse,
+ )
+
+
+class GenerateResourceWithRawResponse:
+ def __init__(self, generate: GenerateResource) -> None:
+ self._generate = generate
+
+ self.create = to_raw_response_wrapper(
+ generate.create,
+ )
+
+
+class AsyncGenerateResourceWithRawResponse:
+ def __init__(self, generate: AsyncGenerateResource) -> None:
+ self._generate = generate
+
+ self.create = async_to_raw_response_wrapper(
+ generate.create,
+ )
+
+
+class GenerateResourceWithStreamingResponse:
+ def __init__(self, generate: GenerateResource) -> None:
+ self._generate = generate
+
+ self.create = to_streamed_response_wrapper(
+ generate.create,
+ )
+
+
+class AsyncGenerateResourceWithStreamingResponse:
+ def __init__(self, generate: AsyncGenerateResource) -> None:
+ self._generate = generate
+
+ self.create = async_to_streamed_response_wrapper(
+ generate.create,
+ )
diff --git a/src/unlayer/resources/convert/convert.py b/src/unlayer/resources/convert/convert.py
index 08eee7c..e4fdfc7 100644
--- a/src/unlayer/resources/convert/convert.py
+++ b/src/unlayer/resources/convert/convert.py
@@ -27,10 +27,12 @@
class ConvertResource(SyncAPIResource):
@cached_property
def full_to_simple(self) -> FullToSimpleResource:
+ """Design schema conversion between Full and Simple formats."""
return FullToSimpleResource(self._client)
@cached_property
def simple_to_full(self) -> SimpleToFullResource:
+ """Design schema conversion between Full and Simple formats."""
return SimpleToFullResource(self._client)
@cached_property
@@ -56,10 +58,12 @@ def with_streaming_response(self) -> ConvertResourceWithStreamingResponse:
class AsyncConvertResource(AsyncAPIResource):
@cached_property
def full_to_simple(self) -> AsyncFullToSimpleResource:
+ """Design schema conversion between Full and Simple formats."""
return AsyncFullToSimpleResource(self._client)
@cached_property
def simple_to_full(self) -> AsyncSimpleToFullResource:
+ """Design schema conversion between Full and Simple formats."""
return AsyncSimpleToFullResource(self._client)
@cached_property
@@ -88,10 +92,12 @@ def __init__(self, convert: ConvertResource) -> None:
@cached_property
def full_to_simple(self) -> FullToSimpleResourceWithRawResponse:
+ """Design schema conversion between Full and Simple formats."""
return FullToSimpleResourceWithRawResponse(self._convert.full_to_simple)
@cached_property
def simple_to_full(self) -> SimpleToFullResourceWithRawResponse:
+ """Design schema conversion between Full and Simple formats."""
return SimpleToFullResourceWithRawResponse(self._convert.simple_to_full)
@@ -101,10 +107,12 @@ def __init__(self, convert: AsyncConvertResource) -> None:
@cached_property
def full_to_simple(self) -> AsyncFullToSimpleResourceWithRawResponse:
+ """Design schema conversion between Full and Simple formats."""
return AsyncFullToSimpleResourceWithRawResponse(self._convert.full_to_simple)
@cached_property
def simple_to_full(self) -> AsyncSimpleToFullResourceWithRawResponse:
+ """Design schema conversion between Full and Simple formats."""
return AsyncSimpleToFullResourceWithRawResponse(self._convert.simple_to_full)
@@ -114,10 +122,12 @@ def __init__(self, convert: ConvertResource) -> None:
@cached_property
def full_to_simple(self) -> FullToSimpleResourceWithStreamingResponse:
+ """Design schema conversion between Full and Simple formats."""
return FullToSimpleResourceWithStreamingResponse(self._convert.full_to_simple)
@cached_property
def simple_to_full(self) -> SimpleToFullResourceWithStreamingResponse:
+ """Design schema conversion between Full and Simple formats."""
return SimpleToFullResourceWithStreamingResponse(self._convert.simple_to_full)
@@ -127,8 +137,10 @@ def __init__(self, convert: AsyncConvertResource) -> None:
@cached_property
def full_to_simple(self) -> AsyncFullToSimpleResourceWithStreamingResponse:
+ """Design schema conversion between Full and Simple formats."""
return AsyncFullToSimpleResourceWithStreamingResponse(self._convert.full_to_simple)
@cached_property
def simple_to_full(self) -> AsyncSimpleToFullResourceWithStreamingResponse:
+ """Design schema conversion between Full and Simple formats."""
return AsyncSimpleToFullResourceWithStreamingResponse(self._convert.simple_to_full)
diff --git a/src/unlayer/resources/convert/full_to_simple.py b/src/unlayer/resources/convert/full_to_simple.py
index d5901f4..35ea241 100644
--- a/src/unlayer/resources/convert/full_to_simple.py
+++ b/src/unlayer/resources/convert/full_to_simple.py
@@ -24,6 +24,8 @@
class FullToSimpleResource(SyncAPIResource):
+ """Design schema conversion between Full and Simple formats."""
+
@cached_property
def with_raw_response(self) -> FullToSimpleResourceWithRawResponse:
"""
@@ -91,6 +93,8 @@ def create(
class AsyncFullToSimpleResource(AsyncAPIResource):
+ """Design schema conversion between Full and Simple formats."""
+
@cached_property
def with_raw_response(self) -> AsyncFullToSimpleResourceWithRawResponse:
"""
diff --git a/src/unlayer/resources/convert/simple_to_full.py b/src/unlayer/resources/convert/simple_to_full.py
index d9b634f..5f22bb0 100644
--- a/src/unlayer/resources/convert/simple_to_full.py
+++ b/src/unlayer/resources/convert/simple_to_full.py
@@ -24,6 +24,8 @@
class SimpleToFullResource(SyncAPIResource):
+ """Design schema conversion between Full and Simple formats."""
+
@cached_property
def with_raw_response(self) -> SimpleToFullResourceWithRawResponse:
"""
@@ -86,6 +88,8 @@ def create(
class AsyncSimpleToFullResource(AsyncAPIResource):
+ """Design schema conversion between Full and Simple formats."""
+
@cached_property
def with_raw_response(self) -> AsyncSimpleToFullResourceWithRawResponse:
"""
diff --git a/src/unlayer/resources/projects.py b/src/unlayer/resources/projects.py
index 59073ac..f842cfe 100644
--- a/src/unlayer/resources/projects.py
+++ b/src/unlayer/resources/projects.py
@@ -5,6 +5,7 @@
import httpx
from .._types import Body, Query, Headers, NotGiven, not_given
+from .._utils import path_template
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
@@ -20,6 +21,8 @@
class ProjectsResource(SyncAPIResource):
+ """Project details and configuration."""
+
@cached_property
def with_raw_response(self) -> ProjectsResourceWithRawResponse:
"""
@@ -65,7 +68,7 @@ def retrieve(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return self._get(
- f"/v3/projects/{id}",
+ path_template("/v3/projects/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
@@ -74,6 +77,8 @@ def retrieve(
class AsyncProjectsResource(AsyncAPIResource):
+ """Project details and configuration."""
+
@cached_property
def with_raw_response(self) -> AsyncProjectsResourceWithRawResponse:
"""
@@ -119,7 +124,7 @@ async def retrieve(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return await self._get(
- f"/v3/projects/{id}",
+ path_template("/v3/projects/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
diff --git a/src/unlayer/resources/templates.py b/src/unlayer/resources/templates.py
index e41c931..75fcee7 100644
--- a/src/unlayer/resources/templates.py
+++ b/src/unlayer/resources/templates.py
@@ -8,7 +8,7 @@
from ..types import template_list_params, template_retrieve_params
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
-from .._utils import maybe_transform, async_maybe_transform
+from .._utils import path_template, maybe_transform, async_maybe_transform
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
@@ -26,6 +26,8 @@
class TemplatesResource(SyncAPIResource):
+ """Template management and retrieval."""
+
@cached_property
def with_raw_response(self) -> TemplatesResourceWithRawResponse:
"""
@@ -74,7 +76,7 @@ def retrieve(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return self._get(
- f"/v3/templates/{id}",
+ path_template("/v3/templates/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers,
extra_query=extra_query,
@@ -148,6 +150,8 @@ def list(
class AsyncTemplatesResource(AsyncAPIResource):
+ """Template management and retrieval."""
+
@cached_property
def with_raw_response(self) -> AsyncTemplatesResourceWithRawResponse:
"""
@@ -196,7 +200,7 @@ async def retrieve(
if not id:
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
return await self._get(
- f"/v3/templates/{id}",
+ path_template("/v3/templates/{id}", id=id),
options=make_request_options(
extra_headers=extra_headers,
extra_query=extra_query,
diff --git a/src/unlayer/resources/workspaces.py b/src/unlayer/resources/workspaces.py
index 3bce476..3ad5619 100644
--- a/src/unlayer/resources/workspaces.py
+++ b/src/unlayer/resources/workspaces.py
@@ -5,6 +5,7 @@
import httpx
from .._types import Body, Query, Headers, NotGiven, not_given
+from .._utils import path_template
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
@@ -21,6 +22,8 @@
class WorkspacesResource(SyncAPIResource):
+ """Workspace access and management."""
+
@cached_property
def with_raw_response(self) -> WorkspacesResourceWithRawResponse:
"""
@@ -68,7 +71,7 @@ def retrieve(
if not workspace_id:
raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}")
return self._get(
- f"/v3/workspaces/{workspace_id}",
+ path_template("/v3/workspaces/{workspace_id}", workspace_id=workspace_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
@@ -100,6 +103,8 @@ def list(
class AsyncWorkspacesResource(AsyncAPIResource):
+ """Workspace access and management."""
+
@cached_property
def with_raw_response(self) -> AsyncWorkspacesResourceWithRawResponse:
"""
@@ -147,7 +152,7 @@ async def retrieve(
if not workspace_id:
raise ValueError(f"Expected a non-empty value for `workspace_id` but received {workspace_id!r}")
return await self._get(
- f"/v3/workspaces/{workspace_id}",
+ path_template("/v3/workspaces/{workspace_id}", workspace_id=workspace_id),
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
diff --git a/src/unlayer/types/ai/__init__.py b/src/unlayer/types/ai/__init__.py
new file mode 100644
index 0000000..199ad74
--- /dev/null
+++ b/src/unlayer/types/ai/__init__.py
@@ -0,0 +1,6 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+from .generate_create_params import GenerateCreateParams as GenerateCreateParams
+from .generate_create_response import GenerateCreateResponse as GenerateCreateResponse
diff --git a/src/unlayer/types/ai/generate_create_params.py b/src/unlayer/types/ai/generate_create_params.py
new file mode 100644
index 0000000..e779f52
--- /dev/null
+++ b/src/unlayer/types/ai/generate_create_params.py
@@ -0,0 +1,94 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+from typing import Dict, Union, Iterable
+from typing_extensions import Literal, Required, Annotated, TypedDict
+
+from ..._types import SequenceNotStr
+from ..._utils import PropertyInfo
+
+__all__ = ["GenerateCreateParams", "Input", "Output", "Context", "ContextCustomTool"]
+
+
+class GenerateCreateParams(TypedDict, total=False):
+ display_mode: Required[Annotated[Literal["email", "web", "popup", "document"], PropertyInfo(alias="displayMode")]]
+ """Display mode for the design"""
+
+ input: Required[Iterable[Input]]
+ """Array of typed input parts (max 50)"""
+
+ output: Required[Output]
+ """What to generate"""
+
+ project_id: Annotated[str, PropertyInfo(alias="projectId")]
+ """The project ID (required for PAT auth, auto-resolved for API key auth)"""
+
+ context: Context
+ """Editor environment context"""
+
+ model: Literal["anthropic/claude-opus-4-6", "openai/gpt-5.2"]
+ """AI model to use, in provider/model format.
+
+ Optional — defaults to anthropic/claude-opus-4-6.
+ """
+
+
+class Input(TypedDict, total=False):
+ type: Required[Literal["text", "prompt", "json", "html", "image"]]
+ """The type of input part"""
+
+ id: str
+ """
+ Predefined prompt ID: SPELLING, EXPAND, SUMMARIZE, REPHRASE, FRIENDLY, FORMAL
+ (for type: "prompt")
+ """
+
+ block_type: Annotated[str, PropertyInfo(alias="blockType")]
+ """Block type of the design data (for type: "json")"""
+
+ data: Union[Dict[str, object], str]
+ """
+ Existing design data (object, for type: "json") or base64 image data (string,
+ for type: "image")
+ """
+
+ html: str
+ """HTML string to import (for type: "html")"""
+
+ schema_version: Annotated[int, PropertyInfo(alias="schemaVersion")]
+ """Design schema version (for type: "json")"""
+
+ text: str
+ """Natural language prompt (for type: "text")"""
+
+ url: str
+ """Image URL to import (for type: "image")"""
+
+
+class Output(TypedDict, total=False):
+ """What to generate"""
+
+ block_type: Required[
+ Annotated[Literal["template", "page", "body", "content", "row", "column"], PropertyInfo(alias="blockType")]
+ ]
+ """The type of design block to generate"""
+
+ type: Required[Literal["json"]]
+ """Output format — currently only "json" is supported"""
+
+
+class ContextCustomTool(TypedDict, total=False, extra_items=object): # type: ignore[call-arg]
+ options: Required[Dict[str, object]]
+
+ slug: Required[str]
+
+
+class Context(TypedDict, total=False, extra_items=object): # type: ignore[call-arg]
+ """Editor environment context"""
+
+ available_tools: Annotated[SequenceNotStr[str], PropertyInfo(alias="availableTools")]
+ """Filter content types available in the generated design"""
+
+ custom_tools: Annotated[Iterable[ContextCustomTool], PropertyInfo(alias="customTools")]
+ """Custom tool declarations with their options"""
diff --git a/src/unlayer/types/ai/generate_create_response.py b/src/unlayer/types/ai/generate_create_response.py
new file mode 100644
index 0000000..669e741
--- /dev/null
+++ b/src/unlayer/types/ai/generate_create_response.py
@@ -0,0 +1,45 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from typing import Dict, Optional
+
+from pydantic import Field as FieldInfo
+
+from ..._models import BaseModel
+
+__all__ = ["GenerateCreateResponse", "Output", "Usage"]
+
+
+class Output(BaseModel):
+ block_type: Optional[str] = FieldInfo(alias="blockType", default=None)
+
+ data: Optional[Dict[str, object]] = None
+ """Generated design data"""
+
+ type: Optional[str] = None
+
+
+class Usage(BaseModel):
+ cached_input_tokens: Optional[int] = FieldInfo(alias="cachedInputTokens", default=None)
+
+ input_tokens: Optional[int] = FieldInfo(alias="inputTokens", default=None)
+
+ output_tokens: Optional[int] = FieldInfo(alias="outputTokens", default=None)
+
+ reasoning_tokens: Optional[int] = FieldInfo(alias="reasoningTokens", default=None)
+
+ total_tokens: Optional[int] = FieldInfo(alias="totalTokens", default=None)
+
+
+class GenerateCreateResponse(BaseModel):
+ """Successfully generated design"""
+
+ id: Optional[str] = None
+ """AI response ID"""
+
+ model: Optional[str] = None
+
+ output: Optional[Output] = None
+
+ provider: Optional[str] = None
+
+ usage: Optional[Usage] = None
diff --git a/src/unlayer/types/convert/full_to_simple_create_params.py b/src/unlayer/types/convert/full_to_simple_create_params.py
index 7038dcf..5d70ae7 100644
--- a/src/unlayer/types/convert/full_to_simple_create_params.py
+++ b/src/unlayer/types/convert/full_to_simple_create_params.py
@@ -2,8 +2,8 @@
from __future__ import annotations
-from typing import Dict, Union
-from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict
+from typing import Dict
+from typing_extensions import Literal, Required, Annotated, TypedDict
from ..._utils import PropertyInfo
@@ -25,12 +25,9 @@ class FullToSimpleCreateParams(TypedDict, total=False):
include_default_values: Annotated[bool, PropertyInfo(alias="includeDefaultValues")]
-class DesignTyped(TypedDict, total=False):
+class Design(TypedDict, total=False, extra_items=object): # type: ignore[call-arg]
body: Required[Dict[str, object]]
counters: Dict[str, object]
schema_version: Annotated[float, PropertyInfo(alias="schemaVersion")]
-
-
-Design: TypeAlias = Union[DesignTyped, Dict[str, object]]
diff --git a/src/unlayer/types/convert/simple_to_full_create_params.py b/src/unlayer/types/convert/simple_to_full_create_params.py
index 247905e..b973376 100644
--- a/src/unlayer/types/convert/simple_to_full_create_params.py
+++ b/src/unlayer/types/convert/simple_to_full_create_params.py
@@ -2,8 +2,8 @@
from __future__ import annotations
-from typing import Dict, Union
-from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict
+from typing import Dict
+from typing_extensions import Literal, Required, Annotated, TypedDict
from ..._utils import PropertyInfo
@@ -24,7 +24,7 @@ class Design_Conversion(TypedDict, total=False):
version: float
-class DesignTyped(TypedDict, total=False):
+class Design(TypedDict, total=False, extra_items=object): # type: ignore[call-arg]
body: Required[Dict[str, object]]
_conversion: Design_Conversion
@@ -32,6 +32,3 @@ class DesignTyped(TypedDict, total=False):
counters: Dict[str, object]
schema_version: Annotated[float, PropertyInfo(alias="schemaVersion")]
-
-
-Design: TypeAlias = Union[DesignTyped, Dict[str, object]]
diff --git a/tests/api_resources/ai/__init__.py b/tests/api_resources/ai/__init__.py
new file mode 100644
index 0000000..fd8019a
--- /dev/null
+++ b/tests/api_resources/ai/__init__.py
@@ -0,0 +1 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
diff --git a/tests/api_resources/ai/test_generate.py b/tests/api_resources/ai/test_generate.py
new file mode 100644
index 0000000..c504af0
--- /dev/null
+++ b/tests/api_resources/ai/test_generate.py
@@ -0,0 +1,184 @@
+# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
+
+from __future__ import annotations
+
+import os
+from typing import Any, cast
+
+import pytest
+
+from unlayer import Unlayer, AsyncUnlayer
+from tests.utils import assert_matches_type
+from unlayer.types.ai import GenerateCreateResponse
+
+base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
+
+
+class TestGenerate:
+ parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
+
+ @parametrize
+ def test_method_create(self, client: Unlayer) -> None:
+ generate = client.ai.generate.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ )
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ def test_method_create_with_all_params(self, client: Unlayer) -> None:
+ generate = client.ai.generate.create(
+ display_mode="email",
+ input=[
+ {
+ "type": "text",
+ "id": "id",
+ "block_type": "blockType",
+ "data": {"foo": "bar"},
+ "html": "html",
+ "schema_version": 0,
+ "text": "text",
+ "url": "url",
+ }
+ ],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ project_id="projectId",
+ context={
+ "available_tools": ["string"],
+ "custom_tools": [
+ {
+ "options": {"foo": "bar"},
+ "slug": "slug",
+ }
+ ],
+ },
+ model="anthropic/claude-opus-4-6",
+ )
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ def test_raw_response_create(self, client: Unlayer) -> None:
+ response = client.ai.generate.with_raw_response.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ )
+
+ assert response.is_closed is True
+ assert response.http_request.headers.get("X-Stainless-Lang") == "python"
+ generate = response.parse()
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ def test_streaming_response_create(self, client: Unlayer) -> None:
+ with client.ai.generate.with_streaming_response.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ ) as response:
+ assert not response.is_closed
+ assert response.http_request.headers.get("X-Stainless-Lang") == "python"
+
+ generate = response.parse()
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ assert cast(Any, response.is_closed) is True
+
+
+class TestAsyncGenerate:
+ parametrize = pytest.mark.parametrize(
+ "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
+ )
+
+ @parametrize
+ async def test_method_create(self, async_client: AsyncUnlayer) -> None:
+ generate = await async_client.ai.generate.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ )
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ async def test_method_create_with_all_params(self, async_client: AsyncUnlayer) -> None:
+ generate = await async_client.ai.generate.create(
+ display_mode="email",
+ input=[
+ {
+ "type": "text",
+ "id": "id",
+ "block_type": "blockType",
+ "data": {"foo": "bar"},
+ "html": "html",
+ "schema_version": 0,
+ "text": "text",
+ "url": "url",
+ }
+ ],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ project_id="projectId",
+ context={
+ "available_tools": ["string"],
+ "custom_tools": [
+ {
+ "options": {"foo": "bar"},
+ "slug": "slug",
+ }
+ ],
+ },
+ model="anthropic/claude-opus-4-6",
+ )
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ async def test_raw_response_create(self, async_client: AsyncUnlayer) -> None:
+ response = await async_client.ai.generate.with_raw_response.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ )
+
+ assert response.is_closed is True
+ assert response.http_request.headers.get("X-Stainless-Lang") == "python"
+ generate = await response.parse()
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ @parametrize
+ async def test_streaming_response_create(self, async_client: AsyncUnlayer) -> None:
+ async with async_client.ai.generate.with_streaming_response.create(
+ display_mode="email",
+ input=[{"type": "text"}],
+ output={
+ "block_type": "template",
+ "type": "json",
+ },
+ ) as response:
+ assert not response.is_closed
+ assert response.http_request.headers.get("X-Stainless-Lang") == "python"
+
+ generate = await response.parse()
+ assert_matches_type(GenerateCreateResponse, generate, path=["response"])
+
+ assert cast(Any, response.is_closed) is True
diff --git a/tests/test_client.py b/tests/test_client.py
index 071a7bc..7137115 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -428,6 +428,30 @@ def test_default_query_option(self) -> None:
client.close()
+ def test_hardcoded_query_params_in_url(self, client: Unlayer) -> None:
+ request = client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true"))
+ url = httpx.URL(request.url)
+ assert dict(url.params) == {"beta": "true"}
+
+ request = client._build_request(
+ FinalRequestOptions(
+ method="get",
+ url="/foo?beta=true",
+ params={"limit": "10", "page": "abc"},
+ )
+ )
+ url = httpx.URL(request.url)
+ assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"}
+
+ request = client._build_request(
+ FinalRequestOptions(
+ method="get",
+ url="/files/a%2Fb?beta=true",
+ params={"limit": "10"},
+ )
+ )
+ assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10"
+
def test_request_extra_json(self, client: Unlayer) -> None:
request = client._build_request(
FinalRequestOptions(
@@ -927,8 +951,14 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
# Test that the proxy environment variables are set correctly
monkeypatch.setenv("HTTPS_PROXY", "https://example.org")
- # Delete in case our environment has this set
+ # Delete in case our environment has any proxy env vars set
monkeypatch.delenv("HTTP_PROXY", raising=False)
+ monkeypatch.delenv("ALL_PROXY", raising=False)
+ monkeypatch.delenv("NO_PROXY", raising=False)
+ monkeypatch.delenv("http_proxy", raising=False)
+ monkeypatch.delenv("https_proxy", raising=False)
+ monkeypatch.delenv("all_proxy", raising=False)
+ monkeypatch.delenv("no_proxy", raising=False)
client = DefaultHttpxClient()
@@ -1303,6 +1333,30 @@ async def test_default_query_option(self) -> None:
await client.close()
+ async def test_hardcoded_query_params_in_url(self, async_client: AsyncUnlayer) -> None:
+ request = async_client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true"))
+ url = httpx.URL(request.url)
+ assert dict(url.params) == {"beta": "true"}
+
+ request = async_client._build_request(
+ FinalRequestOptions(
+ method="get",
+ url="/foo?beta=true",
+ params={"limit": "10", "page": "abc"},
+ )
+ )
+ url = httpx.URL(request.url)
+ assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"}
+
+ request = async_client._build_request(
+ FinalRequestOptions(
+ method="get",
+ url="/files/a%2Fb?beta=true",
+ params={"limit": "10"},
+ )
+ )
+ assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10"
+
def test_request_extra_json(self, client: Unlayer) -> None:
request = client._build_request(
FinalRequestOptions(
@@ -1821,8 +1875,14 @@ async def test_get_platform(self) -> None:
async def test_proxy_environment_variables(self, monkeypatch: pytest.MonkeyPatch) -> None:
# Test that the proxy environment variables are set correctly
monkeypatch.setenv("HTTPS_PROXY", "https://example.org")
- # Delete in case our environment has this set
+ # Delete in case our environment has any proxy env vars set
monkeypatch.delenv("HTTP_PROXY", raising=False)
+ monkeypatch.delenv("ALL_PROXY", raising=False)
+ monkeypatch.delenv("NO_PROXY", raising=False)
+ monkeypatch.delenv("http_proxy", raising=False)
+ monkeypatch.delenv("https_proxy", raising=False)
+ monkeypatch.delenv("all_proxy", raising=False)
+ monkeypatch.delenv("no_proxy", raising=False)
client = DefaultAsyncHttpxClient()
diff --git a/tests/test_deepcopy.py b/tests/test_deepcopy.py
deleted file mode 100644
index f308857..0000000
--- a/tests/test_deepcopy.py
+++ /dev/null
@@ -1,58 +0,0 @@
-from unlayer._utils import deepcopy_minimal
-
-
-def assert_different_identities(obj1: object, obj2: object) -> None:
- assert obj1 == obj2
- assert id(obj1) != id(obj2)
-
-
-def test_simple_dict() -> None:
- obj1 = {"foo": "bar"}
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
-
-
-def test_nested_dict() -> None:
- obj1 = {"foo": {"bar": True}}
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
- assert_different_identities(obj1["foo"], obj2["foo"])
-
-
-def test_complex_nested_dict() -> None:
- obj1 = {"foo": {"bar": [{"hello": "world"}]}}
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
- assert_different_identities(obj1["foo"], obj2["foo"])
- assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"])
- assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0])
-
-
-def test_simple_list() -> None:
- obj1 = ["a", "b", "c"]
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
-
-
-def test_nested_list() -> None:
- obj1 = ["a", [1, 2, 3]]
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
- assert_different_identities(obj1[1], obj2[1])
-
-
-class MyObject: ...
-
-
-def test_ignores_other_types() -> None:
- # custom classes
- my_obj = MyObject()
- obj1 = {"foo": my_obj}
- obj2 = deepcopy_minimal(obj1)
- assert_different_identities(obj1, obj2)
- assert obj1["foo"] is my_obj
-
- # tuples
- obj3 = ("a", "b")
- obj4 = deepcopy_minimal(obj3)
- assert obj3 is obj4
diff --git a/tests/test_extract_files.py b/tests/test_extract_files.py
index 9f2cae2..9bf5e2d 100644
--- a/tests/test_extract_files.py
+++ b/tests/test_extract_files.py
@@ -35,6 +35,15 @@ def test_multiple_files() -> None:
assert query == {"documents": [{}, {}]}
+def test_top_level_file_array() -> None:
+ query = {"files": [b"file one", b"file two"], "title": "hello"}
+ assert extract_files(query, paths=[["files", ""]]) == [
+ ("files[]", b"file one"),
+ ("files[]", b"file two"),
+ ]
+ assert query == {"title": "hello"}
+
+
@pytest.mark.parametrize(
"query,paths,expected",
[
diff --git a/tests/test_files.py b/tests/test_files.py
index f1e9f25..e7c851e 100644
--- a/tests/test_files.py
+++ b/tests/test_files.py
@@ -4,7 +4,8 @@
import pytest
from dirty_equals import IsDict, IsList, IsBytes, IsTuple
-from unlayer._files import to_httpx_files, async_to_httpx_files
+from unlayer._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files
+from unlayer._utils import extract_files
readme_path = Path(__file__).parent.parent.joinpath("README.md")
@@ -49,3 +50,99 @@ def test_string_not_allowed() -> None:
"file": "foo", # type: ignore
}
)
+
+
+def assert_different_identities(obj1: object, obj2: object) -> None:
+ assert obj1 == obj2
+ assert obj1 is not obj2
+
+
+class TestDeepcopyWithPaths:
+ def test_copies_top_level_dict(self) -> None:
+ original = {"file": b"data", "other": "value"}
+ result = deepcopy_with_paths(original, [["file"]])
+ assert_different_identities(result, original)
+
+ def test_file_value_is_same_reference(self) -> None:
+ file_bytes = b"contents"
+ original = {"file": file_bytes}
+ result = deepcopy_with_paths(original, [["file"]])
+ assert_different_identities(result, original)
+ assert result["file"] is file_bytes
+
+ def test_list_popped_wholesale(self) -> None:
+ files = [b"f1", b"f2"]
+ original = {"files": files, "title": "t"}
+ result = deepcopy_with_paths(original, [["files", ""]])
+ assert_different_identities(result, original)
+ result_files = result["files"]
+ assert isinstance(result_files, list)
+ assert_different_identities(result_files, files)
+
+ def test_nested_array_path_copies_list_and_elements(self) -> None:
+ elem1 = {"file": b"f1", "extra": 1}
+ elem2 = {"file": b"f2", "extra": 2}
+ original = {"items": [elem1, elem2]}
+ result = deepcopy_with_paths(original, [["items", "", "file"]])
+ assert_different_identities(result, original)
+ result_items = result["items"]
+ assert isinstance(result_items, list)
+ assert_different_identities(result_items, original["items"])
+ assert_different_identities(result_items[0], elem1)
+ assert_different_identities(result_items[1], elem2)
+
+ def test_empty_paths_returns_same_object(self) -> None:
+ original = {"foo": "bar"}
+ result = deepcopy_with_paths(original, [])
+ assert result is original
+
+ def test_multiple_paths(self) -> None:
+ f1 = b"file1"
+ f2 = b"file2"
+ original = {"a": f1, "b": f2, "c": "unchanged"}
+ result = deepcopy_with_paths(original, [["a"], ["b"]])
+ assert_different_identities(result, original)
+ assert result["a"] is f1
+ assert result["b"] is f2
+ assert result["c"] is original["c"]
+
+ def test_extract_files_does_not_mutate_original_top_level(self) -> None:
+ file_bytes = b"contents"
+ original = {"file": file_bytes, "other": "value"}
+
+ copied = deepcopy_with_paths(original, [["file"]])
+ extracted = extract_files(copied, paths=[["file"]])
+
+ assert extracted == [("file", file_bytes)]
+ assert original == {"file": file_bytes, "other": "value"}
+ assert copied == {"other": "value"}
+
+ def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None:
+ file1 = b"f1"
+ file2 = b"f2"
+ original = {
+ "items": [
+ {"file": file1, "extra": 1},
+ {"file": file2, "extra": 2},
+ ],
+ "title": "example",
+ }
+
+ copied = deepcopy_with_paths(original, [["items", "", "file"]])
+ extracted = extract_files(copied, paths=[["items", "", "file"]])
+
+ assert extracted == [("items[][file]", file1), ("items[][file]", file2)]
+ assert original == {
+ "items": [
+ {"file": file1, "extra": 1},
+ {"file": file2, "extra": 2},
+ ],
+ "title": "example",
+ }
+ assert copied == {
+ "items": [
+ {"extra": 1},
+ {"extra": 2},
+ ],
+ "title": "example",
+ }
diff --git a/tests/test_utils/test_path.py b/tests/test_utils/test_path.py
new file mode 100644
index 0000000..8d48732
--- /dev/null
+++ b/tests/test_utils/test_path.py
@@ -0,0 +1,89 @@
+from __future__ import annotations
+
+from typing import Any
+
+import pytest
+
+from unlayer._utils._path import path_template
+
+
+@pytest.mark.parametrize(
+ "template, kwargs, expected",
+ [
+ ("/v1/{id}", dict(id="abc"), "/v1/abc"),
+ ("/v1/{a}/{b}", dict(a="x", b="y"), "/v1/x/y"),
+ ("/v1/{a}{b}/path/{c}?val={d}#{e}", dict(a="x", b="y", c="z", d="u", e="v"), "/v1/xy/path/z?val=u#v"),
+ ("/{w}/{w}", dict(w="echo"), "/echo/echo"),
+ ("/v1/static", {}, "/v1/static"),
+ ("", {}, ""),
+ ("/v1/?q={n}&count=10", dict(n=42), "/v1/?q=42&count=10"),
+ ("/v1/{v}", dict(v=None), "/v1/null"),
+ ("/v1/{v}", dict(v=True), "/v1/true"),
+ ("/v1/{v}", dict(v=False), "/v1/false"),
+ ("/v1/{v}", dict(v=".hidden"), "/v1/.hidden"), # dot prefix ok
+ ("/v1/{v}", dict(v="file.txt"), "/v1/file.txt"), # dot in middle ok
+ ("/v1/{v}", dict(v="..."), "/v1/..."), # triple dot ok
+ ("/v1/{a}{b}", dict(a=".", b="txt"), "/v1/.txt"), # dot var combining with adjacent to be ok
+ ("/items?q={v}#{f}", dict(v=".", f=".."), "/items?q=.#.."), # dots in query/fragment are fine
+ (
+ "/v1/{a}?query={b}",
+ dict(a="../../other/endpoint", b="a&bad=true"),
+ "/v1/..%2F..%2Fother%2Fendpoint?query=a%26bad%3Dtrue",
+ ),
+ ("/v1/{val}", dict(val="a/b/c"), "/v1/a%2Fb%2Fc"),
+ ("/v1/{val}", dict(val="a/b/c?query=value"), "/v1/a%2Fb%2Fc%3Fquery=value"),
+ ("/v1/{val}", dict(val="a/b/c?query=value&bad=true"), "/v1/a%2Fb%2Fc%3Fquery=value&bad=true"),
+ ("/v1/{val}", dict(val="%20"), "/v1/%2520"), # escapes escape sequences in input
+ # Query: slash and ? are safe, # is not
+ ("/items?q={v}", dict(v="a/b"), "/items?q=a/b"),
+ ("/items?q={v}", dict(v="a?b"), "/items?q=a?b"),
+ ("/items?q={v}", dict(v="a#b"), "/items?q=a%23b"),
+ ("/items?q={v}", dict(v="a b"), "/items?q=a%20b"),
+ # Fragment: slash and ? are safe
+ ("/docs#{v}", dict(v="a/b"), "/docs#a/b"),
+ ("/docs#{v}", dict(v="a?b"), "/docs#a?b"),
+ # Path: slash, ? and # are all encoded
+ ("/v1/{v}", dict(v="a/b"), "/v1/a%2Fb"),
+ ("/v1/{v}", dict(v="a?b"), "/v1/a%3Fb"),
+ ("/v1/{v}", dict(v="a#b"), "/v1/a%23b"),
+ # same var encoded differently by component
+ (
+ "/v1/{v}?q={v}#{v}",
+ dict(v="a/b?c#d"),
+ "/v1/a%2Fb%3Fc%23d?q=a/b?c%23d#a/b?c%23d",
+ ),
+ ("/v1/{val}", dict(val="x?admin=true"), "/v1/x%3Fadmin=true"), # query injection
+ ("/v1/{val}", dict(val="x#admin"), "/v1/x%23admin"), # fragment injection
+ ],
+)
+def test_interpolation(template: str, kwargs: dict[str, Any], expected: str) -> None:
+ assert path_template(template, **kwargs) == expected
+
+
+def test_missing_kwarg_raises_key_error() -> None:
+ with pytest.raises(KeyError, match="org_id"):
+ path_template("/v1/{org_id}")
+
+
+@pytest.mark.parametrize(
+ "template, kwargs",
+ [
+ ("{a}/path", dict(a=".")),
+ ("{a}/path", dict(a="..")),
+ ("/v1/{a}", dict(a=".")),
+ ("/v1/{a}", dict(a="..")),
+ ("/v1/{a}/path", dict(a=".")),
+ ("/v1/{a}/path", dict(a="..")),
+ ("/v1/{a}{b}", dict(a=".", b=".")), # adjacent vars → ".."
+ ("/v1/{a}.", dict(a=".")), # var + static → ".."
+ ("/v1/{a}{b}", dict(a="", b=".")), # empty + dot → "."
+ ("/v1/%2e/{x}", dict(x="ok")), # encoded dot in static text
+ ("/v1/%2e./{x}", dict(x="ok")), # mixed encoded ".." in static
+ ("/v1/.%2E/{x}", dict(x="ok")), # mixed encoded ".." in static
+ ("/v1/{v}?q=1", dict(v="..")),
+ ("/v1/{v}#frag", dict(v="..")),
+ ],
+)
+def test_dot_segment_rejected(template: str, kwargs: dict[str, Any]) -> None:
+ with pytest.raises(ValueError, match="dot-segment"):
+ path_template(template, **kwargs)