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.1.0-alpha.4"
".": "0.1.0-alpha.5"
}
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 4
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-d168b58fcf39dbd0458d132091793d3e2d0930070b7dda2d5f7f1baff20dd31b.yml
openapi_spec_hash: b7e0fd7ee1656d7dbad57209d1584d92
config_hash: c2bc5253d8afd6d67e031f73353c9b22
config_hash: 2d282609080a6011e3f6222451f72237
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.1.0-alpha.5 (2025-05-10)

Full Changelog: [v0.1.0-alpha.4...v0.1.0-alpha.5](https://github.com/onkernel/kernel-python-sdk/compare/v0.1.0-alpha.4...v0.1.0-alpha.5)

### Features

* **api:** update via SDK Studio ([8bceece](https://github.com/onkernel/kernel-python-sdk/commit/8bceece9fb86d9dbc0446abd1018788ff4fbda80))

## 0.1.0-alpha.4 (2025-05-10)

Full Changelog: [v0.1.0-alpha.3...v0.1.0-alpha.4](https://github.com/onkernel/kernel-python-sdk/compare/v0.1.0-alpha.3...v0.1.0-alpha.4)
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ from kernel import Kernel

client = Kernel(
api_key=os.environ.get("KERNEL_API_KEY"), # This is the default and can be omitted
# defaults to "production".
environment="development",
)

response = client.apps.deploy(
Expand All @@ -55,6 +57,8 @@ from kernel import AsyncKernel

client = AsyncKernel(
api_key=os.environ.get("KERNEL_API_KEY"), # This is the default and can be omitted
# defaults to "production".
environment="development",
)


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 = "kernel"
version = "0.1.0-alpha.4"
version = "0.1.0-alpha.5"
description = "The official Python library for the kernel API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
14 changes: 13 additions & 1 deletion src/kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@
from . import types
from ._types import NOT_GIVEN, Omit, NoneType, NotGiven, Transport, ProxiesTypes
from ._utils import file_from_path
from ._client import Client, Kernel, Stream, Timeout, Transport, AsyncClient, AsyncKernel, AsyncStream, RequestOptions
from ._client import (
ENVIRONMENTS,
Client,
Kernel,
Stream,
Timeout,
Transport,
AsyncClient,
AsyncKernel,
AsyncStream,
RequestOptions,
)
from ._models import BaseModel
from ._version import __title__, __version__
from ._response import APIResponse as APIResponse, AsyncAPIResponse as AsyncAPIResponse
Expand Down Expand Up @@ -73,6 +84,7 @@
"AsyncStream",
"Kernel",
"AsyncKernel",
"ENVIRONMENTS",
"file_from_path",
"BaseModel",
"DEFAULT_TIMEOUT",
Expand Down
93 changes: 80 additions & 13 deletions src/kernel/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from __future__ import annotations

import os
from typing import Any, Union, Mapping
from typing_extensions import Self, override
from typing import Any, Dict, Union, Mapping, cast
from typing_extensions import Self, Literal, override

import httpx

Expand All @@ -30,7 +30,22 @@
AsyncAPIClient,
)

__all__ = ["Timeout", "Transport", "ProxiesTypes", "RequestOptions", "Kernel", "AsyncKernel", "Client", "AsyncClient"]
__all__ = [
"ENVIRONMENTS",
"Timeout",
"Transport",
"ProxiesTypes",
"RequestOptions",
"Kernel",
"AsyncKernel",
"Client",
"AsyncClient",
]

ENVIRONMENTS: Dict[str, str] = {
"production": "https://api.onkernel.com/",
"development": "https://localhost:3001/",
}


class Kernel(SyncAPIClient):
Expand All @@ -42,11 +57,14 @@ class Kernel(SyncAPIClient):
# client options
api_key: str

_environment: Literal["production", "development"] | NotGiven

def __init__(
self,
*,
api_key: str | None = None,
base_url: str | httpx.URL | None = None,
environment: Literal["production", "development"] | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
Expand Down Expand Up @@ -77,10 +95,31 @@ def __init__(
)
self.api_key = api_key

if base_url is None:
base_url = os.environ.get("KERNEL_BASE_URL")
if base_url is None:
base_url = f"http://localhost:3001"
self._environment = environment

base_url_env = os.environ.get("KERNEL_BASE_URL")
if is_given(base_url) and base_url is not None:
# cast required because mypy doesn't understand the type narrowing
base_url = cast("str | httpx.URL", base_url) # pyright: ignore[reportUnnecessaryCast]
elif is_given(environment):
if base_url_env and base_url is not None:
raise ValueError(
"Ambiguous URL; The `KERNEL_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None",
)

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
elif base_url_env is not None:
base_url = base_url_env
else:
self._environment = environment = "production"

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc

super().__init__(
version=__version__,
Expand Down Expand Up @@ -122,6 +161,7 @@ def copy(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development"] | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.Client | None = None,
Expand Down Expand Up @@ -157,6 +197,7 @@ def copy(
return self.__class__(
api_key=api_key or self.api_key,
base_url=base_url or self.base_url,
environment=environment or self._environment,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
Expand Down Expand Up @@ -212,11 +253,14 @@ class AsyncKernel(AsyncAPIClient):
# client options
api_key: str

_environment: Literal["production", "development"] | NotGiven

def __init__(
self,
*,
api_key: str | None = None,
base_url: str | httpx.URL | None = None,
environment: Literal["production", "development"] | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
Expand Down Expand Up @@ -247,10 +291,31 @@ def __init__(
)
self.api_key = api_key

if base_url is None:
base_url = os.environ.get("KERNEL_BASE_URL")
if base_url is None:
base_url = f"http://localhost:3001"
self._environment = environment

base_url_env = os.environ.get("KERNEL_BASE_URL")
if is_given(base_url) and base_url is not None:
# cast required because mypy doesn't understand the type narrowing
base_url = cast("str | httpx.URL", base_url) # pyright: ignore[reportUnnecessaryCast]
elif is_given(environment):
if base_url_env and base_url is not None:
raise ValueError(
"Ambiguous URL; The `KERNEL_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None",
)

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
elif base_url_env is not None:
base_url = base_url_env
else:
self._environment = environment = "production"

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc

super().__init__(
version=__version__,
Expand Down Expand Up @@ -292,6 +357,7 @@ def copy(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development"] | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.AsyncClient | None = None,
Expand Down Expand Up @@ -327,6 +393,7 @@ def copy(
return self.__class__(
api_key=api_key or self.api_key,
base_url=base_url or self.base_url,
environment=environment or self._environment,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/_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__ = "kernel"
__version__ = "0.1.0-alpha.4" # x-release-please-version
__version__ = "0.1.0-alpha.5" # x-release-please-version
18 changes: 18 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ def test_base_url_env(self) -> None:
client = Kernel(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

# explicit environment arg requires explicitness
with update_env(KERNEL_BASE_URL="http://localhost:5000/from/env"):
with pytest.raises(ValueError, match=r"you must pass base_url=None"):
Kernel(api_key=api_key, _strict_response_validation=True, environment="production")

client = Kernel(base_url=None, api_key=api_key, _strict_response_validation=True, environment="production")
assert str(client.base_url).startswith("https://api.onkernel.com/")

@pytest.mark.parametrize(
"client",
[
Expand Down Expand Up @@ -1341,6 +1349,16 @@ def test_base_url_env(self) -> None:
client = AsyncKernel(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

# explicit environment arg requires explicitness
with update_env(KERNEL_BASE_URL="http://localhost:5000/from/env"):
with pytest.raises(ValueError, match=r"you must pass base_url=None"):
AsyncKernel(api_key=api_key, _strict_response_validation=True, environment="production")

client = AsyncKernel(
base_url=None, api_key=api_key, _strict_response_validation=True, environment="production"
)
assert str(client.base_url).startswith("https://api.onkernel.com/")

@pytest.mark.parametrize(
"client",
[
Expand Down