Skip to content
Open
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
12 changes: 8 additions & 4 deletions portkey_ai/api_resources/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,10 @@ def copy(
provider=provider or self.provider,
trace_id=trace_id or self.trace_id,
metadata=metadata or self.metadata,
debug=debug or self.debug,
cache_force_refresh=cache_force_refresh or self.cache_force_refresh,
debug=self.debug if debug is None else debug,
cache_force_refresh=self.cache_force_refresh
if cache_force_refresh is None
else cache_force_refresh,
custom_host=custom_host or self.custom_host,
forward_headers=forward_headers or self.forward_headers,
instrumentation=instrumentation or self.instrumentation,
Expand Down Expand Up @@ -621,8 +623,10 @@ def copy(
provider=provider or self.provider,
trace_id=trace_id or self.trace_id,
metadata=metadata or self.metadata,
debug=debug or self.debug,
cache_force_refresh=cache_force_refresh or self.cache_force_refresh,
debug=self.debug if debug is None else debug,
cache_force_refresh=self.cache_force_refresh
if cache_force_refresh is None
else cache_force_refresh,
custom_host=custom_host or self.custom_host,
forward_headers=forward_headers or self.forward_headers,
instrumentation=instrumentation or self.instrumentation,
Expand Down
93 changes: 93 additions & 0 deletions tests/test_client_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from typing import List, Optional

import httpx
import pytest

from portkey_ai import AsyncPortkey, Portkey


@pytest.fixture
def requests() -> List[httpx.Request]:
return []


@pytest.fixture
def transport(requests: List[httpx.Request]) -> httpx.MockTransport:
def respond(request: httpx.Request) -> httpx.Response:
requests.append(request)
return httpx.Response(200, json={"id": "test", "choices": []})

return httpx.MockTransport(respond)


@pytest.mark.parametrize("method", ["copy", "with_options"])
@pytest.mark.parametrize(
"initial,override,expected",
[
(True, False, False),
(True, None, True),
(False, None, False),
(False, True, True),
],
)
def test_copy_boolean_headers(
method: str,
initial: bool,
override: Optional[bool],
expected: bool,
transport: httpx.MockTransport,
requests: List[httpx.Request],
) -> None:
with httpx.Client(transport=transport) as http_client:
client = Portkey(
api_key="test-key",
http_client=http_client,
debug=initial,
cache_force_refresh=initial,
)
copied = getattr(client, method)(debug=override, cache_force_refresh=override)
copied.chat.completions.create(model="test-model", messages=[])
client.chat.completions.create(model="test-model", messages=[])

for header in ("x-portkey-debug", "x-portkey-cache-force-refresh"):
assert [request.headers[header] for request in requests] == [
str(expected).lower(),
str(initial).lower(),
]


@pytest.mark.asyncio
@pytest.mark.parametrize("method", ["copy", "with_options"])
@pytest.mark.parametrize(
"initial,override,expected",
[
(True, False, False),
(True, None, True),
(False, None, False),
(False, True, True),
],
)
async def test_async_copy_boolean_headers(
method: str,
initial: bool,
override: Optional[bool],
expected: bool,
transport: httpx.MockTransport,
requests: List[httpx.Request],
) -> None:
async with httpx.AsyncClient(transport=transport) as http_client:
client = AsyncPortkey(
api_key="test-key",
http_client=http_client,
debug=initial,
cache_force_refresh=initial,
)
copied = getattr(client, method)(debug=override, cache_force_refresh=override)
await copied.chat.completions.create(model="test-model", messages=[])
await client.chat.completions.create(model="test-model", messages=[])

for header in ("x-portkey-debug", "x-portkey-cache-force-refresh"):
assert [request.headers[header] for request in requests] == [
str(expected).lower(),
str(initial).lower(),
]