diff --git a/portkey_ai/api_resources/client.py b/portkey_ai/api_resources/client.py index 4558d2a3..393d008e 100644 --- a/portkey_ai/api_resources/client.py +++ b/portkey_ai/api_resources/client.py @@ -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, @@ -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, diff --git a/tests/test_client_copy.py b/tests/test_client_copy.py new file mode 100644 index 00000000..733bcd80 --- /dev/null +++ b/tests/test_client_copy.py @@ -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(), + ]