from __future__ import annotations

import gc
import os
import sys
import json
import asyncio
import inspect
import dataclasses
import tracemalloc
from typing import Any, Union, TypeVar, Callable, Iterable, Iterator, Optional, Coroutine, cast
from unittest import mock
from typing_extensions import Literal, AsyncIterator, override

import httpx2
import pytest
from pydantic import ValidationError

from openai import OpenAI, AsyncOpenAI, OpenAIError, APIResponseValidationError
from openai.auth import SubjectTokenWorkloadIdentity
from tests.respx2 import MockRouter
from openai._types import Omit
from openai._utils import asyncify
from openai._models import BaseModel, FinalRequestOptions
from openai._streaming import Stream, AsyncStream
from openai._exceptions import APIStatusError, APITimeoutError, APIResponseValidationError
from openai._base_client import (
    DEFAULT_TIMEOUT,
    HTTPX_DEFAULT_TIMEOUT,
    BaseClient,
    OtherPlatform,
    DefaultHttpxClient,
    DefaultAsyncHttpxClient,
    get_platform,
    make_request_options,
)
from tests.respx2.models import Call as MockRequestCall

from .utils import update_env

T = TypeVar("T")
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
api_key = "My API Key"
admin_api_key = "My Admin API Key"
workload_identity: SubjectTokenWorkloadIdentity = {
    "identity_provider_id": "provider_123",
    "service_account_id": "service_account_123",
    "provider": {
        "get_token": lambda: "external-subject-token",
        "token_type": "jwt",
    },
}


def _get_params(client: BaseClient[Any, Any]) -> dict[str, str]:
    request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
    url = httpx2.URL(request.url)
    return dict(url.params)


def _low_retry_timeout(*_args: Any, **_kwargs: Any) -> float:
    return 0.1


def mirror_request_content(request: httpx2.Request) -> httpx2.Response:
    return httpx2.Response(200, content=request.content)


# note: we can't use the httpx.MockTransport class as it consumes the request
#       body itself, which means we can't test that the body is read lazily
class MockTransport(httpx2.BaseTransport, httpx2.AsyncBaseTransport):
    def __init__(
        self,
        handler: Callable[[httpx2.Request], httpx2.Response]
        | Callable[[httpx2.Request], Coroutine[Any, Any, httpx2.Response]],
    ) -> None:
        self.handler = handler

    @override
    def handle_request(
        self,
        request: httpx2.Request,
    ) -> httpx2.Response:
        assert not inspect.iscoroutinefunction(self.handler), "handler must not be a coroutine function"
        assert inspect.isfunction(self.handler), "handler must be a function"
        return self.handler(request)

    @override
    async def handle_async_request(
        self,
        request: httpx2.Request,
    ) -> httpx2.Response:
        assert inspect.iscoroutinefunction(self.handler), "handler must be a coroutine function"
        return await self.handler(request)


@dataclasses.dataclass
class Counter:
    value: int = 0


def _make_sync_iterator(iterable: Iterable[T], counter: Optional[Counter] = None) -> Iterator[T]:
    for item in iterable:
        if counter:
            counter.value += 1
        yield item


async def _make_async_iterator(iterable: Iterable[T], counter: Optional[Counter] = None) -> AsyncIterator[T]:
    for item in iterable:
        if counter:
            counter.value += 1
        yield item


def _get_open_connections(client: OpenAI | AsyncOpenAI) -> int:
    transport = client._client._transport
    if isinstance(transport, httpx2.HTTPTransport) or isinstance(transport, httpx2.AsyncHTTPTransport):
        return len(transport._pool._requests)

    assert type(transport).__module__ == "httpx2"
    return len(cast(Any, transport)._pool._requests)


class TestOpenAI:
    @pytest.mark.respx2(base_url=base_url)
    def test_raw_response(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = client.post("/foo", cast_to=httpx2.Response)
        assert response.status_code == 200
        assert type(response).__module__ == os.environ.get("OPENAI_TEST_HTTP_CLIENT", "httpx2")
        assert response.json() == {"foo": "bar"}

    @pytest.mark.respx2(base_url=base_url)
    def test_raw_response_for_binary(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/foo").mock(
            return_value=httpx2.Response(200, headers={"Content-Type": "application/binary"}, content='{"foo": "bar"}')
        )

        response = client.post("/foo", cast_to=httpx2.Response)
        assert response.status_code == 200
        assert type(response).__module__ == os.environ.get("OPENAI_TEST_HTTP_CLIENT", "httpx2")
        assert response.json() == {"foo": "bar"}

    def test_copy(self, client: OpenAI) -> None:
        copied = client.copy()
        assert id(copied) != id(client)

        copied = client.copy(api_key="another My API Key")
        assert copied.api_key == "another My API Key"
        assert client.api_key == "My API Key"

        copied = client.copy(admin_api_key="another My Admin API Key")
        assert copied.admin_api_key == "another My Admin API Key"
        assert client.admin_api_key == "My Admin API Key"

    def test_copy_default_options(self, client: OpenAI) -> None:
        # options that have a default are overridden correctly
        copied = client.copy(max_retries=7)
        assert copied.max_retries == 7
        assert client.max_retries == 2

        copied2 = copied.copy(max_retries=6)
        assert copied2.max_retries == 6
        assert copied.max_retries == 7

        # timeout
        assert isinstance(client.timeout, httpx2.Timeout)
        copied = client.copy(timeout=None)
        assert copied.timeout is None
        assert isinstance(client.timeout, httpx2.Timeout)

    def test_copy_default_headers(self) -> None:
        client = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={"X-Foo": "bar"},
        )
        assert client.default_headers["X-Foo"] == "bar"

        # does not override the already given value when not specified
        copied = client.copy()
        assert copied.default_headers["X-Foo"] == "bar"

        # merges already given headers
        copied = client.copy(default_headers={"X-Bar": "openai"})
        assert copied.default_headers["X-Foo"] == "bar"
        assert copied.default_headers["X-Bar"] == "openai"

        # uses new values for any already given headers
        copied = client.copy(default_headers={"X-Foo": "openai"})
        assert copied.default_headers["X-Foo"] == "openai"

        # set_default_headers

        # completely overrides already set values
        copied = client.copy(set_default_headers={})
        assert copied.default_headers.get("X-Foo") is None

        copied = client.copy(set_default_headers={"X-Bar": "Robert"})
        assert copied.default_headers["X-Bar"] == "Robert"

        with pytest.raises(
            ValueError,
            match="`default_headers` and `set_default_headers` arguments are mutually exclusive",
        ):
            client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"})
        client.close()

    def test_copy_default_query(self) -> None:
        client = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_query={"foo": "bar"},
        )
        assert _get_params(client)["foo"] == "bar"

        # does not override the already given value when not specified
        copied = client.copy()
        assert _get_params(copied)["foo"] == "bar"

        # merges already given params
        copied = client.copy(default_query={"bar": "openai"})
        params = _get_params(copied)
        assert params["foo"] == "bar"
        assert params["bar"] == "openai"

        # uses new values for any already given headers
        copied = client.copy(default_query={"foo": "openai"})
        assert _get_params(copied)["foo"] == "openai"

        # set_default_query

        # completely overrides already set values
        copied = client.copy(set_default_query={})
        assert _get_params(copied) == {}

        copied = client.copy(set_default_query={"bar": "Robert"})
        assert _get_params(copied)["bar"] == "Robert"

        with pytest.raises(
            ValueError,
            # TODO: update
            match="`default_query` and `set_default_query` arguments are mutually exclusive",
        ):
            client.copy(set_default_query={}, default_query={"foo": "Bar"})

        client.close()

    def test_copy_signature(self, client: OpenAI) -> None:
        # ensure the same parameters that can be passed to the client are defined in the `.copy()` method
        init_signature = inspect.signature(
            # mypy doesn't like that we access the `__init__` property.
            client.__init__,  # type: ignore[misc]
        )
        copy_signature = inspect.signature(client.copy)
        exclude_params = {"transport", "proxies", "_strict_response_validation"}

        for name in init_signature.parameters.keys():
            if name in exclude_params:
                continue

            copy_param = copy_signature.parameters.get(name)
            assert copy_param is not None, f"copy() signature is missing the {name} param"

    @pytest.mark.skipif(sys.version_info >= (3, 10), reason="fails because of a memory leak that started from 3.12")
    def test_copy_build_request(self, client: OpenAI) -> None:
        options = FinalRequestOptions(method="get", url="/foo")

        def build_request(options: FinalRequestOptions) -> None:
            client_copy = client.copy()
            client_copy._build_request(options)

        # ensure that the machinery is warmed up before tracing starts.
        build_request(options)
        gc.collect()

        tracemalloc.start(1000)

        snapshot_before = tracemalloc.take_snapshot()

        ITERATIONS = 10
        for _ in range(ITERATIONS):
            build_request(options)

        gc.collect()
        snapshot_after = tracemalloc.take_snapshot()

        tracemalloc.stop()

        def add_leak(leaks: list[tracemalloc.StatisticDiff], diff: tracemalloc.StatisticDiff) -> None:
            if diff.count == 0:
                # Avoid false positives by considering only leaks (i.e. allocations that persist).
                return

            if diff.count % ITERATIONS != 0:
                # Avoid false positives by considering only leaks that appear per iteration.
                return

            for frame in diff.traceback:
                if any(
                    frame.filename.endswith(fragment)
                    for fragment in [
                        # to_raw_response_wrapper leaks through the @functools.wraps() decorator.
                        #
                        # removing the decorator fixes the leak for reasons we don't understand.
                        "openai/_legacy_response.py",
                        "openai/_response.py",
                        # pydantic.BaseModel.model_dump || pydantic.BaseModel.dict leak memory for some reason.
                        "openai/_compat.py",
                        # Standard library leaks we don't care about.
                        "/logging/__init__.py",
                    ]
                ):
                    return

            leaks.append(diff)

        leaks: list[tracemalloc.StatisticDiff] = []
        for diff in snapshot_after.compare_to(snapshot_before, "traceback"):
            add_leak(leaks, diff)
        if leaks:
            for leak in leaks:
                print("MEMORY LEAK:", leak)
                for frame in leak.traceback:
                    print(frame)
            raise AssertionError()

    def test_request_timeout(self, client: OpenAI) -> None:
        request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == DEFAULT_TIMEOUT

        request = client._build_request(FinalRequestOptions(method="get", url="/foo", timeout=httpx2.Timeout(100.0)))
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == httpx2.Timeout(100.0)

    def test_client_timeout_option(self) -> None:
        client = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            timeout=httpx2.Timeout(0),
        )

        request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == httpx2.Timeout(0)

        client.close()

    def test_http_client_timeout_option(self) -> None:
        # custom timeout given to the httpx client should be used
        with httpx2.Client(timeout=None) as http_client:
            client = OpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == httpx2.Timeout(None)

            client.close()

        # no timeout given to the httpx client should not use the httpx default
        with httpx2.Client() as http_client:
            client = OpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == DEFAULT_TIMEOUT

            client.close()

        # explicitly passing the default timeout currently results in it being ignored
        with httpx2.Client(timeout=HTTPX_DEFAULT_TIMEOUT) as http_client:
            client = OpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == DEFAULT_TIMEOUT  # our default

            client.close()

    async def test_invalid_http_client(self) -> None:
        with pytest.raises(TypeError, match="Invalid `http_client` arg"):
            async with httpx2.AsyncClient() as http_client:
                OpenAI(
                    base_url=base_url,
                    api_key=api_key,
                    admin_api_key=admin_api_key,
                    _strict_response_validation=True,
                    http_client=cast(Any, http_client),
                )

    def test_default_headers_option(self) -> None:
        test_client = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={"X-Foo": "bar"},
        )
        request = test_client._build_request(FinalRequestOptions(method="get", url="/foo"))
        assert request.headers.get("x-foo") == "bar"
        assert request.headers.get("x-stainless-lang") == "python"

        test_client2 = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={
                "X-Foo": "openai",
                "X-Stainless-Lang": "my-overriding-header",
            },
        )
        request = test_client2._build_request(FinalRequestOptions(method="get", url="/foo"))
        assert request.headers.get("x-foo") == "openai"
        assert request.headers.get("x-stainless-lang") == "my-overriding-header"

        test_client.close()
        test_client2.close()

    def test_validate_headers(self) -> None:
        client = OpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        options = client._prepare_options(FinalRequestOptions(method="get", url="/foo"))
        request = client._build_request(options)

        assert request.headers.get("Authorization") == f"Bearer {api_key}"

        admin_request = client._build_request(
            FinalRequestOptions(
                method="get",
                url="/organization/projects",
                security={"admin_api_key_auth": True},
            )
        )
        assert admin_request.headers.get("Authorization") == f"Bearer {admin_api_key}"

        with update_env(**{"OPENAI_API_KEY": Omit()}):
            admin_only = OpenAI(
                base_url=base_url,
                api_key=None,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            )
            admin_only_request = admin_only._build_request(
                FinalRequestOptions(
                    method="get",
                    url="/organization/projects",
                    security={"admin_api_key_auth": True},
                )
            )
            assert admin_only_request.headers.get("Authorization") == f"Bearer {admin_api_key}"

            with pytest.raises(
                TypeError,
                match="Could not resolve authentication method",
            ):
                admin_only._build_request(
                    FinalRequestOptions(
                        method="post",
                        url="/responses",
                        security={"bearer_auth": True},
                    )
                )

        with update_env(
            **{
                "OPENAI_API_KEY": Omit(),
                "OPENAI_ADMIN_KEY": Omit(),
            }
        ):
            no_credentials = OpenAI(
                base_url=base_url,
                api_key=None,
                admin_api_key=None,
                _enforce_credentials=False,
                _strict_response_validation=True,
            )
            lowercase_auth_request = no_credentials._build_request(
                FinalRequestOptions(method="get", url="/foo", headers={"authorization": "Bearer custom"})
            )
            assert lowercase_auth_request.headers.get("Authorization") == "Bearer custom"

            omitted_auth_request = no_credentials._build_request(
                FinalRequestOptions(method="get", url="/foo", headers={"authorization": Omit()})
            )
            assert "Authorization" not in omitted_auth_request.headers

        with update_env(
            **{
                "OPENAI_API_KEY": Omit(),
                "OPENAI_ADMIN_KEY": Omit(),
            }
        ):
            with pytest.raises(OpenAIError, match="Missing credentials"):
                OpenAI(base_url=base_url, api_key=None, admin_api_key=None, _strict_response_validation=True)

    @pytest.mark.respx2(base_url=base_url)
    def test_api_key_provider_preserves_admin_auth(self, respx2_mock: MockRouter) -> None:
        respx2_mock.get("/organization/projects").mock(return_value=httpx2.Response(200, json={"ok": True}))

        provider_called = False

        def api_key_provider() -> str:
            nonlocal provider_called
            provider_called = True
            return "dynamic-api-key"

        client = OpenAI(base_url=base_url, api_key=api_key_provider, admin_api_key=admin_api_key)
        response = client.get(
            "/organization/projects",
            cast_to=httpx2.Response,
            options={"security": {"admin_api_key_auth": True}},
        )

        assert response.request.headers.get("Authorization") == f"Bearer {admin_api_key}"
        assert provider_called is False

    def test_api_key_provider_does_not_fill_admin_auth(self) -> None:
        provider_called = False

        def api_key_provider() -> str:
            nonlocal provider_called
            provider_called = True
            return "dynamic-api-key"

        with update_env(OPENAI_ADMIN_KEY=Omit()):
            client = OpenAI(base_url=base_url, api_key=api_key_provider, admin_api_key=None)
            with pytest.raises(TypeError, match="Could not resolve authentication method"):
                client.get(
                    "/organization/projects",
                    cast_to=httpx2.Response,
                    options={"security": {"admin_api_key_auth": True}},
                )

        assert provider_called is False

    @pytest.mark.respx2(base_url=base_url)
    def test_workload_identity_preserves_admin_auth(self, respx2_mock: MockRouter) -> None:
        respx2_mock.get("/organization/projects").mock(return_value=httpx2.Response(200, json={"ok": True}))

        client = OpenAI(base_url=base_url, workload_identity=workload_identity, admin_api_key=admin_api_key)
        response = client.get(
            "/organization/projects",
            cast_to=httpx2.Response,
            options={"security": {"admin_api_key_auth": True}},
        )

        assert response.request.headers.get("Authorization") == f"Bearer {admin_api_key}"

    def test_workload_identity_is_mutually_exclusive_with_api_key(self) -> None:
        with pytest.raises(
            OpenAIError,
            match="The `api_key` and `workload_identity` arguments are mutually exclusive",
        ):
            OpenAI(
                base_url=base_url,
                api_key=api_key,
                workload_identity=workload_identity,  # type: ignore[reportArgumentType]
                organization="org_123",
                _strict_response_validation=True,
            )

    def test_default_query_option(self) -> None:
        client = OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_query={"query_param": "bar"},
        )
        request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
        url = httpx2.URL(request.url)
        assert dict(url.params) == {"query_param": "bar"}

        request = client._build_request(
            FinalRequestOptions(
                method="get",
                url="/foo",
                params={"foo": "baz", "query_param": "overridden"},
            )
        )
        url = httpx2.URL(request.url)
        assert dict(url.params) == {"foo": "baz", "query_param": "overridden"}

        client.close()

    def test_hardcoded_query_params_in_url(self, client: OpenAI) -> None:
        request = client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true"))
        url = httpx2.URL(str(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 = httpx2.URL(str(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: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
                extra_json={"baz": False},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"foo": "bar", "baz": False}

        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                extra_json={"baz": False},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"baz": False}

        # `extra_json` takes priority over `json_data` when keys clash
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar", "baz": True},
                extra_json={"baz": None},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"foo": "bar", "baz": None}

    def test_request_extra_headers(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(extra_headers={"X-Foo": "Foo"}),
            ),
        )
        assert request.headers.get("X-Foo") == "Foo"

        # `extra_headers` takes priority over `default_headers` when keys clash
        request = client.with_options(default_headers={"X-Bar": "true"})._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    extra_headers={"X-Bar": "false"},
                ),
            ),
        )
        assert request.headers.get("X-Bar") == "false"

    def test_request_extra_query(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    extra_query={"my_query_param": "Foo"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"my_query_param": "Foo"}

        # if both `query` and `extra_query` are given, they are merged
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    query={"bar": "1"},
                    extra_query={"foo": "2"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"bar": "1", "foo": "2"}

        # `extra_query` takes priority over `query` when keys clash
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    query={"foo": "1"},
                    extra_query={"foo": "2"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"foo": "2"}

    def test_multipart_repeating_array(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions.construct(
                method="post",
                url="/foo",
                headers={"Content-Type": "multipart/form-data; boundary=6b7ba517decee4a450543ea6ae821c82"},
                json_data={"array": ["foo", "bar"]},
                files=[("foo.txt", b"hello world")],
            )
        )

        assert request.read().split(b"\r\n") == [
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="array[]"',
            b"",
            b"foo",
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="array[]"',
            b"",
            b"bar",
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="foo.txt"; filename="upload"',
            b"Content-Type: application/octet-stream",
            b"",
            b"hello world",
            b"--6b7ba517decee4a450543ea6ae821c82--",
            b"",
        ]

    @pytest.mark.respx2(base_url=base_url)
    def test_binary_content_upload(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/upload").mock(side_effect=mirror_request_content)

        file_content = b"Hello, this is a test file."

        response = client.post(
            "/upload",
            content=file_content,
            cast_to=httpx2.Response,
            options={"headers": {"Content-Type": "application/octet-stream"}},
        )

        assert response.status_code == 200
        assert response.request.headers["Content-Type"] == "application/octet-stream"
        assert response.content == file_content

    def test_binary_content_upload_with_iterator(self) -> None:
        file_content = b"Hello, this is a test file."
        counter = Counter()
        iterator = _make_sync_iterator([file_content], counter=counter)

        def mock_handler(request: httpx2.Request) -> httpx2.Response:
            assert counter.value == 0, "the request body should not have been read"
            return httpx2.Response(200, content=request.read())

        with OpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            http_client=httpx2.Client(transport=MockTransport(handler=mock_handler)),
        ) as client:
            response = client.post(
                "/upload",
                content=iterator,
                cast_to=httpx2.Response,
                options={"headers": {"Content-Type": "application/octet-stream"}},
            )

            assert response.status_code == 200
            assert response.request.headers["Content-Type"] == "application/octet-stream"
            assert response.content == file_content
            assert counter.value == 1

    @pytest.mark.respx2(base_url=base_url)
    def test_binary_content_upload_with_body_is_deprecated(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/upload").mock(side_effect=mirror_request_content)

        file_content = b"Hello, this is a test file."

        with pytest.deprecated_call(
            match="Passing raw bytes as `body` is deprecated and will be removed in a future version. Please pass raw bytes via the `content` parameter instead."
        ):
            response = client.post(
                "/upload",
                body=file_content,
                cast_to=httpx2.Response,
                options={"headers": {"Content-Type": "application/octet-stream"}},
            )

        assert response.status_code == 200
        assert response.request.headers["Content-Type"] == "application/octet-stream"
        assert response.content == file_content

    @pytest.mark.respx2(base_url=base_url)
    def test_basic_union_response(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        class Model1(BaseModel):
            name: str

        class Model2(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model2)
        assert response.foo == "bar"

    @pytest.mark.respx2(base_url=base_url)
    def test_union_response_different_types(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        """Union of objects with the same field name using a different type"""

        class Model1(BaseModel):
            foo: int

        class Model2(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model2)
        assert response.foo == "bar"

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": 1}))

        response = client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model1)
        assert response.foo == 1

    @pytest.mark.respx2(base_url=base_url)
    def test_non_application_json_content_type_for_json_data(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        """
        Response that sets Content-Type to something other than application/json but returns json data
        """

        class Model(BaseModel):
            foo: int

        respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(
                200,
                content=json.dumps({"foo": 2}),
                headers={"Content-Type": "application/text"},
            )
        )

        response = client.get("/foo", cast_to=Model)
        assert isinstance(response, Model)
        assert response.foo == 2

    def test_base_url_setter(self) -> None:
        client = OpenAI(
            base_url="https://example.com/from_init",
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
        )
        assert client.base_url == "https://example.com/from_init/"

        client.base_url = "https://example.com/from_setter"  # type: ignore[assignment]

        assert client.base_url == "https://example.com/from_setter/"

        client.close()

    def test_base_url_env(self) -> None:
        with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
            client = OpenAI(api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True)
            assert client.base_url == "http://localhost:5000/from/env/"

    @pytest.mark.parametrize(
        "client",
        [
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.Client(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    def test_base_url_trailing_slash(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "http://localhost:5000/custom/path/foo"
        client.close()

    @pytest.mark.parametrize(
        "client",
        [
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.Client(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    def test_base_url_no_trailing_slash(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "http://localhost:5000/custom/path/foo"
        client.close()

    @pytest.mark.parametrize(
        "client",
        [
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            OpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.Client(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    def test_absolute_request_url(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="https://myapi.com/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "https://myapi.com/foo"
        client.close()

    def test_copied_client_does_not_close_http(self) -> None:
        test_client = OpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        assert not test_client.is_closed()

        copied = test_client.copy()
        assert copied is not test_client

        del copied

        assert not test_client.is_closed()

    def test_client_context_manager(self) -> None:
        test_client = OpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        with test_client as c2:
            assert c2 is test_client
            assert not c2.is_closed()
            assert not test_client.is_closed()
        assert test_client.is_closed()

    @pytest.mark.respx2(base_url=base_url)
    def test_client_response_validation_error(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        class Model(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": {"invalid": True}}))

        with pytest.raises(APIResponseValidationError) as exc:
            client.get("/foo", cast_to=Model)

        assert isinstance(exc.value.__cause__, ValidationError)

    def test_client_max_retries_validation(self) -> None:
        with pytest.raises(TypeError, match=r"max_retries cannot be None"):
            OpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                max_retries=cast(Any, None),
            )

    @pytest.mark.respx2(base_url=base_url)
    def test_default_stream_cls(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        class Model(BaseModel):
            name: str

        respx2_mock.post("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        stream = client.post("/foo", cast_to=Model, stream=True, stream_cls=Stream[Model])
        assert isinstance(stream, Stream)
        stream.response.close()

    @pytest.mark.respx2(base_url=base_url)
    def test_received_text_for_expected_json(self, respx2_mock: MockRouter) -> None:
        class Model(BaseModel):
            name: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, text="my-custom-format"))

        strict_client = OpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )

        with pytest.raises(APIResponseValidationError):
            strict_client.get("/foo", cast_to=Model)

        non_strict_client = OpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=False
        )

        response = non_strict_client.get("/foo", cast_to=Model)
        assert isinstance(response, str)  # type: ignore[unreachable]

        strict_client.close()
        non_strict_client.close()

    @pytest.mark.parametrize(
        "remaining_retries,retry_after,timeout",
        [
            [3, "20", 20],
            [3, "0", 0.5],
            [3, "-10", 0.5],
            [3, "60", 60],
            [3, "61", 61],
            [3, "120", 120],
            [3, "121", 0.5],
            [3, "Fri, 29 Sep 2023 16:26:57 GMT", 20],
            [3, "Fri, 29 Sep 2023 16:26:37 GMT", 0.5],
            [3, "Fri, 29 Sep 2023 16:26:27 GMT", 0.5],
            [3, "Fri, 29 Sep 2023 16:27:37 GMT", 60],
            [3, "Fri, 29 Sep 2023 16:27:38 GMT", 61],
            [3, "Fri, 29 Sep 2023 16:28:37 GMT", 120],
            [3, "Fri, 29 Sep 2023 16:28:38 GMT", 0.5],
            [3, "99999999999999999999999999999999999", 0.5],
            [3, "inf", 0.5],
            [3, "nan", 0.5],
            [3, "Zun, 29 Sep 2023 16:26:27 GMT", 0.5],
            [3, "Fri, 29 Sep 100000 16:26:57 GMT", 0.5],
            [3, "", 0.5],
            [2, "", 0.5 * 2.0],
            [1, "", 0.5 * 4.0],
            [-1100, "", 8],  # test large number potentially overflowing
        ],
    )
    @mock.patch("time.time", mock.MagicMock(return_value=1696004797))
    def test_parse_retry_after_header(
        self, remaining_retries: int, retry_after: str, timeout: float, client: OpenAI
    ) -> None:
        headers = httpx2.Headers({"retry-after": retry_after})
        options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
        calculated = client._calculate_retry_timeout(remaining_retries, options, headers)
        assert calculated == pytest.approx(timeout, 0.5 * 0.875)  # pyright: ignore[reportUnknownMemberType]

    @pytest.mark.parametrize(
        "headers,should_retry",
        [
            [{"retry-after": "120"}, True],
            [{"retry-after": "121"}, False],
            [{"retry-after-ms": "120000"}, True],
            [{"retry-after-ms": "120001"}, False],
            [{"retry-after": "Fri, 29 Sep 2023 16:28:37 GMT"}, True],
            [{"retry-after": "Fri, 29 Sep 2023 16:28:38 GMT"}, False],
        ],
    )
    @mock.patch("time.time", mock.MagicMock(return_value=1696004797))
    def test_retry_after_max_delay(self, headers: dict[str, str], should_retry: bool, client: OpenAI) -> None:
        response = httpx2.Response(429, headers=headers)
        assert client._should_retry(response) is should_retry

    @pytest.mark.respx2(base_url=base_url)
    def test_does_not_retry_retry_after_above_max(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        route = respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(429, headers={"retry-after": "121"}, json={"error": {}})
        )

        with pytest.raises(APIStatusError):
            client.get("/foo", cast_to=httpx2.Response)

        assert route.call_count == 1

    @pytest.mark.respx2(base_url=base_url)
    def test_invalid_retry_after_date_does_not_mask_status_error(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        route = respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(
                400,
                headers={"retry-after": "Fri, 29 Sep 100000 16:26:57 GMT"},
                json={"error": {}},
            )
        )

        with pytest.raises(APIStatusError):
            client.get("/foo", cast_to=httpx2.Response)

        assert route.call_count == 1

    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    def test_retrying_timeout_errors_doesnt_leak(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/chat/completions").mock(side_effect=httpx2.TimeoutException("Test timeout error"))

        with pytest.raises(APITimeoutError):
            client.chat.completions.with_streaming_response.create(
                messages=[
                    {
                        "content": "string",
                        "role": "developer",
                    }
                ],
                model="gpt-5.4",
            ).__enter__()

        assert _get_open_connections(client) == 0

    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    def test_retrying_status_errors_doesnt_leak(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        respx2_mock.post("/chat/completions").mock(return_value=httpx2.Response(500))

        with pytest.raises(APIStatusError):
            client.chat.completions.with_streaming_response.create(
                messages=[
                    {
                        "content": "string",
                        "role": "developer",
                    }
                ],
                model="gpt-5.4",
            ).__enter__()
        assert _get_open_connections(client) == 0

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    @pytest.mark.parametrize("failure_mode", ["status", "exception"])
    def test_retries_taken(
        self,
        client: OpenAI,
        failures_before_success: int,
        failure_mode: Literal["status", "exception"],
        respx2_mock: MockRouter,
    ) -> None:
        client = client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                if failure_mode == "exception":
                    raise RuntimeError("oops")
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
        )

        assert response.retries_taken == failures_before_success
        assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    def test_omit_retry_count_header(
        self, client: OpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
            extra_headers={"x-stainless-retry-count": Omit()},
        )

        assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    def test_overwrite_retry_count_header(
        self, client: OpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
            extra_headers={"x-stainless-retry-count": "42"},
        )

        assert response.http_request.headers.get("x-stainless-retry-count") == "42"

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    def test_retries_taken_new_response_class(
        self, client: OpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        with client.chat.completions.with_streaming_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
        ) as response:
            assert response.retries_taken == failures_before_success
            assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

    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 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()

        mounts = tuple(client._mounts.items())
        assert len(mounts) == 1
        assert mounts[0][0].pattern == "https://"

    @pytest.mark.filterwarnings("ignore:.*deprecated.*:DeprecationWarning")
    def test_default_client_creation(self) -> None:
        # Ensure that the client can be initialized without any exceptions
        DefaultHttpxClient(
            verify=True,
            cert=None,
            trust_env=True,
            http1=True,
            http2=False,
            limits=httpx2.Limits(max_connections=100, max_keepalive_connections=20),
        )

    @pytest.mark.respx2(base_url=base_url)
    def test_follow_redirects(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        # Test that the default follow_redirects=True allows following redirects
        respx2_mock.post("/redirect").mock(
            return_value=httpx2.Response(302, headers={"Location": f"{base_url}/redirected"})
        )
        respx2_mock.get("/redirected").mock(return_value=httpx2.Response(200, json={"status": "ok"}))

        response = client.post("/redirect", body={"key": "value"}, cast_to=httpx2.Response)
        assert response.status_code == 200
        assert response.json() == {"status": "ok"}

    @pytest.mark.respx2(base_url=base_url)
    def test_follow_redirects_disabled(self, respx2_mock: MockRouter, client: OpenAI) -> None:
        # Test that follow_redirects=False prevents following redirects
        respx2_mock.post("/redirect").mock(
            return_value=httpx2.Response(302, headers={"Location": f"{base_url}/redirected"})
        )

        with pytest.raises(APIStatusError) as exc_info:
            client.post(
                "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx2.Response
            )

        assert exc_info.value.response.status_code == 302
        assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

    def test_api_key_before_after_refresh_provider(self) -> None:
        client = OpenAI(base_url=base_url, api_key=lambda: "test_bearer_token")

        assert client.api_key == ""
        assert "Authorization" not in client.auth_headers

        client._refresh_api_key()

        assert client.api_key == "test_bearer_token"
        assert client.auth_headers.get("Authorization") == "Bearer test_bearer_token"

    def test_api_key_before_after_refresh_str(self) -> None:
        client = OpenAI(base_url=base_url, api_key="test_api_key")

        assert client.auth_headers.get("Authorization") == "Bearer test_api_key"
        client._refresh_api_key()

        assert client.auth_headers.get("Authorization") == "Bearer test_api_key"

    @pytest.mark.respx2()
    def test_api_key_refresh_on_retry(self, respx2_mock: MockRouter) -> None:
        respx2_mock.post(base_url + "/chat/completions").mock(
            side_effect=[
                httpx2.Response(500, json={"error": "server error"}),
                httpx2.Response(200, json={"foo": "bar"}),
            ]
        )

        counter = 0

        def token_provider() -> str:
            nonlocal counter

            counter += 1

            if counter == 1:
                return "first"

            return "second"

        client = OpenAI(base_url=base_url, api_key=token_provider)
        client.chat.completions.create(messages=[], model="gpt-4")

        calls = cast("list[MockRequestCall]", respx2_mock.calls)
        assert len(calls) == 2

        assert calls[0].request.headers.get("Authorization") == "Bearer first"
        assert calls[1].request.headers.get("Authorization") == "Bearer second"

    def test_copy_auth(self) -> None:
        client = OpenAI(base_url=base_url, api_key=lambda: "test_bearer_token_1").copy(
            api_key=lambda: "test_bearer_token_2"
        )
        client._refresh_api_key()
        assert client.auth_headers == {"Authorization": "Bearer test_bearer_token_2"}


class TestAsyncOpenAI:
    @pytest.mark.respx2(base_url=base_url)
    async def test_raw_response(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        respx2_mock.post("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = await async_client.post("/foo", cast_to=httpx2.Response)
        assert response.status_code == 200
        assert type(response).__module__ == os.environ.get("OPENAI_TEST_HTTP_CLIENT", "httpx2")
        assert response.json() == {"foo": "bar"}

    @pytest.mark.respx2(base_url=base_url)
    async def test_raw_response_for_binary(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        respx2_mock.post("/foo").mock(
            return_value=httpx2.Response(200, headers={"Content-Type": "application/binary"}, content='{"foo": "bar"}')
        )

        response = await async_client.post("/foo", cast_to=httpx2.Response)
        assert response.status_code == 200
        assert type(response).__module__ == os.environ.get("OPENAI_TEST_HTTP_CLIENT", "httpx2")
        assert response.json() == {"foo": "bar"}

    def test_copy(self, async_client: AsyncOpenAI) -> None:
        copied = async_client.copy()
        assert id(copied) != id(async_client)

        copied = async_client.copy(api_key="another My API Key")
        assert copied.api_key == "another My API Key"
        assert async_client.api_key == "My API Key"

        copied = async_client.copy(admin_api_key="another My Admin API Key")
        assert copied.admin_api_key == "another My Admin API Key"
        assert async_client.admin_api_key == "My Admin API Key"

    def test_copy_default_options(self, async_client: AsyncOpenAI) -> None:
        # options that have a default are overridden correctly
        copied = async_client.copy(max_retries=7)
        assert copied.max_retries == 7
        assert async_client.max_retries == 2

        copied2 = copied.copy(max_retries=6)
        assert copied2.max_retries == 6
        assert copied.max_retries == 7

        # timeout
        assert isinstance(async_client.timeout, httpx2.Timeout)
        copied = async_client.copy(timeout=None)
        assert copied.timeout is None
        assert isinstance(async_client.timeout, httpx2.Timeout)

    async def test_copy_default_headers(self) -> None:
        client = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={"X-Foo": "bar"},
        )
        assert client.default_headers["X-Foo"] == "bar"

        # does not override the already given value when not specified
        copied = client.copy()
        assert copied.default_headers["X-Foo"] == "bar"

        # merges already given headers
        copied = client.copy(default_headers={"X-Bar": "openai"})
        assert copied.default_headers["X-Foo"] == "bar"
        assert copied.default_headers["X-Bar"] == "openai"

        # uses new values for any already given headers
        copied = client.copy(default_headers={"X-Foo": "openai"})
        assert copied.default_headers["X-Foo"] == "openai"

        # set_default_headers

        # completely overrides already set values
        copied = client.copy(set_default_headers={})
        assert copied.default_headers.get("X-Foo") is None

        copied = client.copy(set_default_headers={"X-Bar": "Robert"})
        assert copied.default_headers["X-Bar"] == "Robert"

        with pytest.raises(
            ValueError,
            match="`default_headers` and `set_default_headers` arguments are mutually exclusive",
        ):
            client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"})
        await client.close()

    async def test_copy_default_query(self) -> None:
        client = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_query={"foo": "bar"},
        )
        assert _get_params(client)["foo"] == "bar"

        # does not override the already given value when not specified
        copied = client.copy()
        assert _get_params(copied)["foo"] == "bar"

        # merges already given params
        copied = client.copy(default_query={"bar": "openai"})
        params = _get_params(copied)
        assert params["foo"] == "bar"
        assert params["bar"] == "openai"

        # uses new values for any already given headers
        copied = client.copy(default_query={"foo": "openai"})
        assert _get_params(copied)["foo"] == "openai"

        # set_default_query

        # completely overrides already set values
        copied = client.copy(set_default_query={})
        assert _get_params(copied) == {}

        copied = client.copy(set_default_query={"bar": "Robert"})
        assert _get_params(copied)["bar"] == "Robert"

        with pytest.raises(
            ValueError,
            # TODO: update
            match="`default_query` and `set_default_query` arguments are mutually exclusive",
        ):
            client.copy(set_default_query={}, default_query={"foo": "Bar"})

        await client.close()

    def test_copy_signature(self, async_client: AsyncOpenAI) -> None:
        # ensure the same parameters that can be passed to the client are defined in the `.copy()` method
        init_signature = inspect.signature(
            # mypy doesn't like that we access the `__init__` property.
            async_client.__init__,  # type: ignore[misc]
        )
        copy_signature = inspect.signature(async_client.copy)
        exclude_params = {"transport", "proxies", "_strict_response_validation"}

        for name in init_signature.parameters.keys():
            if name in exclude_params:
                continue

            copy_param = copy_signature.parameters.get(name)
            assert copy_param is not None, f"copy() signature is missing the {name} param"

    @pytest.mark.skipif(sys.version_info >= (3, 10), reason="fails because of a memory leak that started from 3.12")
    def test_copy_build_request(self, async_client: AsyncOpenAI) -> None:
        options = FinalRequestOptions(method="get", url="/foo")

        def build_request(options: FinalRequestOptions) -> None:
            client_copy = async_client.copy()
            client_copy._build_request(options)

        # ensure that the machinery is warmed up before tracing starts.
        build_request(options)
        gc.collect()

        tracemalloc.start(1000)

        snapshot_before = tracemalloc.take_snapshot()

        ITERATIONS = 10
        for _ in range(ITERATIONS):
            build_request(options)

        gc.collect()
        snapshot_after = tracemalloc.take_snapshot()

        tracemalloc.stop()

        def add_leak(leaks: list[tracemalloc.StatisticDiff], diff: tracemalloc.StatisticDiff) -> None:
            if diff.count == 0:
                # Avoid false positives by considering only leaks (i.e. allocations that persist).
                return

            if diff.count % ITERATIONS != 0:
                # Avoid false positives by considering only leaks that appear per iteration.
                return

            for frame in diff.traceback:
                if any(
                    frame.filename.endswith(fragment)
                    for fragment in [
                        # to_raw_response_wrapper leaks through the @functools.wraps() decorator.
                        #
                        # removing the decorator fixes the leak for reasons we don't understand.
                        "openai/_legacy_response.py",
                        "openai/_response.py",
                        # pydantic.BaseModel.model_dump || pydantic.BaseModel.dict leak memory for some reason.
                        "openai/_compat.py",
                        # Standard library leaks we don't care about.
                        "/logging/__init__.py",
                    ]
                ):
                    return

            leaks.append(diff)

        leaks: list[tracemalloc.StatisticDiff] = []
        for diff in snapshot_after.compare_to(snapshot_before, "traceback"):
            add_leak(leaks, diff)
        if leaks:
            for leak in leaks:
                print("MEMORY LEAK:", leak)
                for frame in leak.traceback:
                    print(frame)
            raise AssertionError()

    async def test_request_timeout(self, async_client: AsyncOpenAI) -> None:
        request = async_client._build_request(FinalRequestOptions(method="get", url="/foo"))
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == DEFAULT_TIMEOUT

        request = async_client._build_request(
            FinalRequestOptions(method="get", url="/foo", timeout=httpx2.Timeout(100.0))
        )
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == httpx2.Timeout(100.0)

    async def test_client_timeout_option(self) -> None:
        client = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            timeout=httpx2.Timeout(0),
        )

        request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
        timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
        assert timeout == httpx2.Timeout(0)

        await client.close()

    async def test_http_client_timeout_option(self) -> None:
        # custom timeout given to the httpx client should be used
        async with httpx2.AsyncClient(timeout=None) as http_client:
            client = AsyncOpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == httpx2.Timeout(None)

            await client.close()

        # no timeout given to the httpx client should not use the httpx default
        async with httpx2.AsyncClient() as http_client:
            client = AsyncOpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == DEFAULT_TIMEOUT

            await client.close()

        # explicitly passing the default timeout currently results in it being ignored
        async with httpx2.AsyncClient(timeout=HTTPX_DEFAULT_TIMEOUT) as http_client:
            client = AsyncOpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=http_client,
            )

            request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
            timeout = httpx2.Timeout(**request.extensions["timeout"])  # type: ignore
            assert timeout == DEFAULT_TIMEOUT  # our default

            await client.close()

    def test_invalid_http_client(self) -> None:
        with pytest.raises(TypeError, match="Invalid `http_client` arg"):
            with httpx2.Client() as http_client:
                AsyncOpenAI(
                    base_url=base_url,
                    api_key=api_key,
                    admin_api_key=admin_api_key,
                    _strict_response_validation=True,
                    http_client=cast(Any, http_client),
                )

    async def test_default_headers_option(self) -> None:
        test_client = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={"X-Foo": "bar"},
        )
        request = test_client._build_request(FinalRequestOptions(method="get", url="/foo"))
        assert request.headers.get("x-foo") == "bar"
        assert request.headers.get("x-stainless-lang") == "python"

        test_client2 = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_headers={
                "X-Foo": "openai",
                "X-Stainless-Lang": "my-overriding-header",
            },
        )
        request = test_client2._build_request(FinalRequestOptions(method="get", url="/foo"))
        assert request.headers.get("x-foo") == "openai"
        assert request.headers.get("x-stainless-lang") == "my-overriding-header"

        await test_client.close()
        await test_client2.close()

    async def test_validate_headers(self) -> None:
        client = AsyncOpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        options = await client._prepare_options(FinalRequestOptions(method="get", url="/foo"))
        request = client._build_request(options)
        assert request.headers.get("Authorization") == f"Bearer {api_key}"

        admin_request = client._build_request(
            FinalRequestOptions(
                method="get",
                url="/organization/projects",
                security={"admin_api_key_auth": True},
            )
        )
        assert admin_request.headers.get("Authorization") == f"Bearer {admin_api_key}"

        with update_env(**{"OPENAI_API_KEY": Omit()}):
            admin_only = AsyncOpenAI(
                base_url=base_url,
                api_key=None,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            )
            admin_only_request = admin_only._build_request(
                FinalRequestOptions(
                    method="get",
                    url="/organization/projects",
                    security={"admin_api_key_auth": True},
                )
            )
            assert admin_only_request.headers.get("Authorization") == f"Bearer {admin_api_key}"

            with pytest.raises(
                TypeError,
                match="Could not resolve authentication method",
            ):
                admin_only._build_request(
                    FinalRequestOptions(
                        method="post",
                        url="/responses",
                        security={"bearer_auth": True},
                    )
                )

        with update_env(
            **{
                "OPENAI_API_KEY": Omit(),
                "OPENAI_ADMIN_KEY": Omit(),
            }
        ):
            no_credentials = AsyncOpenAI(
                base_url=base_url,
                api_key=None,
                admin_api_key=None,
                _enforce_credentials=False,
                _strict_response_validation=True,
            )
            lowercase_auth_request = no_credentials._build_request(
                FinalRequestOptions(method="get", url="/foo", headers={"authorization": "Bearer custom"})
            )
            assert lowercase_auth_request.headers.get("Authorization") == "Bearer custom"

            omitted_auth_request = no_credentials._build_request(
                FinalRequestOptions(method="get", url="/foo", headers={"authorization": Omit()})
            )
            assert "Authorization" not in omitted_auth_request.headers

        with update_env(
            **{
                "OPENAI_API_KEY": Omit(),
                "OPENAI_ADMIN_KEY": Omit(),
            }
        ):
            with pytest.raises(OpenAIError, match="Missing credentials"):
                AsyncOpenAI(base_url=base_url, api_key=None, admin_api_key=None, _strict_response_validation=True)

    @pytest.mark.respx2(base_url=base_url)
    async def test_api_key_provider_preserves_admin_auth(self, respx2_mock: MockRouter) -> None:
        respx2_mock.get("/organization/projects").mock(return_value=httpx2.Response(200, json={"ok": True}))

        provider_called = False

        async def api_key_provider() -> str:
            nonlocal provider_called
            provider_called = True
            return "dynamic-api-key"

        client = AsyncOpenAI(base_url=base_url, api_key=api_key_provider, admin_api_key=admin_api_key)
        response = await client.get(
            "/organization/projects",
            cast_to=httpx2.Response,
            options={"security": {"admin_api_key_auth": True}},
        )

        assert response.request.headers.get("Authorization") == f"Bearer {admin_api_key}"
        assert provider_called is False

    async def test_api_key_provider_does_not_fill_admin_auth(self) -> None:
        provider_called = False

        async def api_key_provider() -> str:
            nonlocal provider_called
            provider_called = True
            return "dynamic-api-key"

        with update_env(OPENAI_ADMIN_KEY=Omit()):
            client = AsyncOpenAI(base_url=base_url, api_key=api_key_provider, admin_api_key=None)
            with pytest.raises(TypeError, match="Could not resolve authentication method"):
                await client.get(
                    "/organization/projects",
                    cast_to=httpx2.Response,
                    options={"security": {"admin_api_key_auth": True}},
                )

        assert provider_called is False

    @pytest.mark.respx2(base_url=base_url)
    async def test_workload_identity_preserves_admin_auth(self, respx2_mock: MockRouter) -> None:
        respx2_mock.get("/organization/projects").mock(return_value=httpx2.Response(200, json={"ok": True}))

        client = AsyncOpenAI(base_url=base_url, workload_identity=workload_identity, admin_api_key=admin_api_key)
        response = await client.get(
            "/organization/projects",
            cast_to=httpx2.Response,
            options={"security": {"admin_api_key_auth": True}},
        )

        assert response.request.headers.get("Authorization") == f"Bearer {admin_api_key}"

    async def test_default_query_option(self) -> None:
        client = AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            default_query={"query_param": "bar"},
        )
        request = client._build_request(FinalRequestOptions(method="get", url="/foo"))
        url = httpx2.URL(request.url)
        assert dict(url.params) == {"query_param": "bar"}

        request = client._build_request(
            FinalRequestOptions(
                method="get",
                url="/foo",
                params={"foo": "baz", "query_param": "overridden"},
            )
        )
        url = httpx2.URL(request.url)
        assert dict(url.params) == {"foo": "baz", "query_param": "overridden"}

        await client.close()

    async def test_hardcoded_query_params_in_url(self, async_client: AsyncOpenAI) -> None:
        request = async_client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true"))
        url = httpx2.URL(str(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 = httpx2.URL(str(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: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
                extra_json={"baz": False},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"foo": "bar", "baz": False}

        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                extra_json={"baz": False},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"baz": False}

        # `extra_json` takes priority over `json_data` when keys clash
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar", "baz": True},
                extra_json={"baz": None},
            ),
        )
        data = json.loads(request.content.decode("utf-8"))
        assert data == {"foo": "bar", "baz": None}

    def test_request_extra_headers(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(extra_headers={"X-Foo": "Foo"}),
            ),
        )
        assert request.headers.get("X-Foo") == "Foo"

        # `extra_headers` takes priority over `default_headers` when keys clash
        request = client.with_options(default_headers={"X-Bar": "true"})._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    extra_headers={"X-Bar": "false"},
                ),
            ),
        )
        assert request.headers.get("X-Bar") == "false"

    def test_request_extra_query(self, client: OpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    extra_query={"my_query_param": "Foo"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"my_query_param": "Foo"}

        # if both `query` and `extra_query` are given, they are merged
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    query={"bar": "1"},
                    extra_query={"foo": "2"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"bar": "1", "foo": "2"}

        # `extra_query` takes priority over `query` when keys clash
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                **make_request_options(
                    query={"foo": "1"},
                    extra_query={"foo": "2"},
                ),
            ),
        )
        params = dict(request.url.params)
        assert params == {"foo": "2"}

    def test_multipart_repeating_array(self, async_client: AsyncOpenAI) -> None:
        request = async_client._build_request(
            FinalRequestOptions.construct(
                method="post",
                url="/foo",
                headers={"Content-Type": "multipart/form-data; boundary=6b7ba517decee4a450543ea6ae821c82"},
                json_data={"array": ["foo", "bar"]},
                files=[("foo.txt", b"hello world")],
            )
        )

        assert request.read().split(b"\r\n") == [
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="array[]"',
            b"",
            b"foo",
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="array[]"',
            b"",
            b"bar",
            b"--6b7ba517decee4a450543ea6ae821c82",
            b'Content-Disposition: form-data; name="foo.txt"; filename="upload"',
            b"Content-Type: application/octet-stream",
            b"",
            b"hello world",
            b"--6b7ba517decee4a450543ea6ae821c82--",
            b"",
        ]

    @pytest.mark.respx2(base_url=base_url)
    async def test_binary_content_upload(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        respx2_mock.post("/upload").mock(side_effect=mirror_request_content)

        file_content = b"Hello, this is a test file."

        response = await async_client.post(
            "/upload",
            content=file_content,
            cast_to=httpx2.Response,
            options={"headers": {"Content-Type": "application/octet-stream"}},
        )

        assert response.status_code == 200
        assert response.request.headers["Content-Type"] == "application/octet-stream"
        assert response.content == file_content

    async def test_binary_content_upload_with_asynciterator(self) -> None:
        file_content = b"Hello, this is a test file."
        counter = Counter()
        iterator = _make_async_iterator([file_content], counter=counter)

        async def mock_handler(request: httpx2.Request) -> httpx2.Response:
            assert counter.value == 0, "the request body should not have been read"
            return httpx2.Response(200, content=await request.aread())

        async with AsyncOpenAI(
            base_url=base_url,
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
            http_client=httpx2.AsyncClient(transport=MockTransport(handler=mock_handler)),
        ) as client:
            response = await client.post(
                "/upload",
                content=iterator,
                cast_to=httpx2.Response,
                options={"headers": {"Content-Type": "application/octet-stream"}},
            )

            assert response.status_code == 200
            assert response.request.headers["Content-Type"] == "application/octet-stream"
            assert response.content == file_content
            assert counter.value == 1

    @pytest.mark.respx2(base_url=base_url)
    async def test_binary_content_upload_with_body_is_deprecated(
        self, respx2_mock: MockRouter, async_client: AsyncOpenAI
    ) -> None:
        respx2_mock.post("/upload").mock(side_effect=mirror_request_content)

        file_content = b"Hello, this is a test file."

        with pytest.deprecated_call(
            match="Passing raw bytes as `body` is deprecated and will be removed in a future version. Please pass raw bytes via the `content` parameter instead."
        ):
            response = await async_client.post(
                "/upload",
                body=file_content,
                cast_to=httpx2.Response,
                options={"headers": {"Content-Type": "application/octet-stream"}},
            )

        assert response.status_code == 200
        assert response.request.headers["Content-Type"] == "application/octet-stream"
        assert response.content == file_content

    @pytest.mark.respx2(base_url=base_url)
    async def test_basic_union_response(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        class Model1(BaseModel):
            name: str

        class Model2(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = await async_client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model2)
        assert response.foo == "bar"

    @pytest.mark.respx2(base_url=base_url)
    async def test_union_response_different_types(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        """Union of objects with the same field name using a different type"""

        class Model1(BaseModel):
            foo: int

        class Model2(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        response = await async_client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model2)
        assert response.foo == "bar"

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": 1}))

        response = await async_client.get("/foo", cast_to=cast(Any, Union[Model1, Model2]))
        assert isinstance(response, Model1)
        assert response.foo == 1

    @pytest.mark.respx2(base_url=base_url)
    async def test_non_application_json_content_type_for_json_data(
        self, respx2_mock: MockRouter, async_client: AsyncOpenAI
    ) -> None:
        """
        Response that sets Content-Type to something other than application/json but returns json data
        """

        class Model(BaseModel):
            foo: int

        respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(
                200,
                content=json.dumps({"foo": 2}),
                headers={"Content-Type": "application/text"},
            )
        )

        response = await async_client.get("/foo", cast_to=Model)
        assert isinstance(response, Model)
        assert response.foo == 2

    async def test_base_url_setter(self) -> None:
        client = AsyncOpenAI(
            base_url="https://example.com/from_init",
            api_key=api_key,
            admin_api_key=admin_api_key,
            _strict_response_validation=True,
        )
        assert client.base_url == "https://example.com/from_init/"

        client.base_url = "https://example.com/from_setter"  # type: ignore[assignment]

        assert client.base_url == "https://example.com/from_setter/"

        await client.close()

    async def test_base_url_env(self) -> None:
        with update_env(OPENAI_BASE_URL="http://localhost:5000/from/env"):
            client = AsyncOpenAI(api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True)
            assert client.base_url == "http://localhost:5000/from/env/"

    @pytest.mark.parametrize(
        "client",
        [
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.AsyncClient(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    async def test_base_url_trailing_slash(self, client: AsyncOpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "http://localhost:5000/custom/path/foo"
        await client.close()

    @pytest.mark.parametrize(
        "client",
        [
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.AsyncClient(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    async def test_base_url_no_trailing_slash(self, client: AsyncOpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "http://localhost:5000/custom/path/foo"
        await client.close()

    @pytest.mark.parametrize(
        "client",
        [
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
            ),
            AsyncOpenAI(
                base_url="http://localhost:5000/custom/path/",
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                http_client=httpx2.AsyncClient(),
            ),
        ],
        ids=["standard", "custom http client"],
    )
    async def test_absolute_request_url(self, client: AsyncOpenAI) -> None:
        request = client._build_request(
            FinalRequestOptions(
                method="post",
                url="https://myapi.com/foo",
                json_data={"foo": "bar"},
            ),
        )
        assert request.url == "https://myapi.com/foo"
        await client.close()

    async def test_copied_client_does_not_close_http(self) -> None:
        test_client = AsyncOpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        assert not test_client.is_closed()

        copied = test_client.copy()
        assert copied is not test_client

        del copied

        await asyncio.sleep(0.2)
        assert not test_client.is_closed()

    async def test_client_context_manager(self) -> None:
        test_client = AsyncOpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )
        async with test_client as c2:
            assert c2 is test_client
            assert not c2.is_closed()
            assert not test_client.is_closed()
        assert test_client.is_closed()

    @pytest.mark.respx2(base_url=base_url)
    async def test_client_response_validation_error(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        class Model(BaseModel):
            foo: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, json={"foo": {"invalid": True}}))

        with pytest.raises(APIResponseValidationError) as exc:
            await async_client.get("/foo", cast_to=Model)

        assert isinstance(exc.value.__cause__, ValidationError)

    async def test_client_max_retries_validation(self) -> None:
        with pytest.raises(TypeError, match=r"max_retries cannot be None"):
            AsyncOpenAI(
                base_url=base_url,
                api_key=api_key,
                admin_api_key=admin_api_key,
                _strict_response_validation=True,
                max_retries=cast(Any, None),
            )

    @pytest.mark.respx2(base_url=base_url)
    async def test_default_stream_cls(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        class Model(BaseModel):
            name: str

        respx2_mock.post("/foo").mock(return_value=httpx2.Response(200, json={"foo": "bar"}))

        stream = await async_client.post("/foo", cast_to=Model, stream=True, stream_cls=AsyncStream[Model])
        assert isinstance(stream, AsyncStream)
        await stream.response.aclose()

    @pytest.mark.respx2(base_url=base_url)
    async def test_received_text_for_expected_json(self, respx2_mock: MockRouter) -> None:
        class Model(BaseModel):
            name: str

        respx2_mock.get("/foo").mock(return_value=httpx2.Response(200, text="my-custom-format"))

        strict_client = AsyncOpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=True
        )

        with pytest.raises(APIResponseValidationError):
            await strict_client.get("/foo", cast_to=Model)

        non_strict_client = AsyncOpenAI(
            base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=False
        )

        response = await non_strict_client.get("/foo", cast_to=Model)
        assert isinstance(response, str)  # type: ignore[unreachable]

        await strict_client.close()
        await non_strict_client.close()

    @pytest.mark.parametrize(
        "remaining_retries,retry_after,timeout",
        [
            [3, "20", 20],
            [3, "0", 0.5],
            [3, "-10", 0.5],
            [3, "60", 60],
            [3, "61", 61],
            [3, "120", 120],
            [3, "121", 0.5],
            [3, "Fri, 29 Sep 2023 16:26:57 GMT", 20],
            [3, "Fri, 29 Sep 2023 16:26:37 GMT", 0.5],
            [3, "Fri, 29 Sep 2023 16:26:27 GMT", 0.5],
            [3, "Fri, 29 Sep 2023 16:27:37 GMT", 60],
            [3, "Fri, 29 Sep 2023 16:27:38 GMT", 61],
            [3, "Fri, 29 Sep 2023 16:28:37 GMT", 120],
            [3, "Fri, 29 Sep 2023 16:28:38 GMT", 0.5],
            [3, "99999999999999999999999999999999999", 0.5],
            [3, "inf", 0.5],
            [3, "nan", 0.5],
            [3, "Zun, 29 Sep 2023 16:26:27 GMT", 0.5],
            [3, "Fri, 29 Sep 100000 16:26:57 GMT", 0.5],
            [3, "", 0.5],
            [2, "", 0.5 * 2.0],
            [1, "", 0.5 * 4.0],
            [-1100, "", 8],  # test large number potentially overflowing
        ],
    )
    @mock.patch("time.time", mock.MagicMock(return_value=1696004797))
    async def test_parse_retry_after_header(
        self, remaining_retries: int, retry_after: str, timeout: float, async_client: AsyncOpenAI
    ) -> None:
        headers = httpx2.Headers({"retry-after": retry_after})
        options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
        calculated = async_client._calculate_retry_timeout(remaining_retries, options, headers)
        assert calculated == pytest.approx(timeout, 0.5 * 0.875)  # pyright: ignore[reportUnknownMemberType]

    @pytest.mark.respx2(base_url=base_url)
    async def test_does_not_retry_retry_after_above_max(
        self, respx2_mock: MockRouter, async_client: AsyncOpenAI
    ) -> None:
        route = respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(429, headers={"retry-after": "121"}, json={"error": {}})
        )

        with pytest.raises(APIStatusError):
            await async_client.get("/foo", cast_to=httpx2.Response)

        assert route.call_count == 1

    @pytest.mark.respx2(base_url=base_url)
    async def test_invalid_retry_after_date_does_not_mask_status_error(
        self, respx2_mock: MockRouter, async_client: AsyncOpenAI
    ) -> None:
        route = respx2_mock.get("/foo").mock(
            return_value=httpx2.Response(
                400,
                headers={"retry-after": "Fri, 29 Sep 100000 16:26:57 GMT"},
                json={"error": {}},
            )
        )

        with pytest.raises(APIStatusError):
            await async_client.get("/foo", cast_to=httpx2.Response)

        assert route.call_count == 1

    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    async def test_retrying_timeout_errors_doesnt_leak(
        self, respx2_mock: MockRouter, async_client: AsyncOpenAI
    ) -> None:
        respx2_mock.post("/chat/completions").mock(side_effect=httpx2.TimeoutException("Test timeout error"))

        with pytest.raises(APITimeoutError):
            await async_client.chat.completions.with_streaming_response.create(
                messages=[
                    {
                        "content": "string",
                        "role": "developer",
                    }
                ],
                model="gpt-5.4",
            ).__aenter__()

        assert _get_open_connections(async_client) == 0

    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    async def test_retrying_status_errors_doesnt_leak(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        respx2_mock.post("/chat/completions").mock(return_value=httpx2.Response(500))

        with pytest.raises(APIStatusError):
            await async_client.chat.completions.with_streaming_response.create(
                messages=[
                    {
                        "content": "string",
                        "role": "developer",
                    }
                ],
                model="gpt-5.4",
            ).__aenter__()
        assert _get_open_connections(async_client) == 0

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    @pytest.mark.parametrize("failure_mode", ["status", "exception"])
    async def test_retries_taken(
        self,
        async_client: AsyncOpenAI,
        failures_before_success: int,
        failure_mode: Literal["status", "exception"],
        respx2_mock: MockRouter,
    ) -> None:
        client = async_client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                if failure_mode == "exception":
                    raise RuntimeError("oops")
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = await client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
        )

        assert response.retries_taken == failures_before_success
        assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    async def test_omit_retry_count_header(
        self, async_client: AsyncOpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = async_client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = await client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
            extra_headers={"x-stainless-retry-count": Omit()},
        )

        assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    async def test_overwrite_retry_count_header(
        self, async_client: AsyncOpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = async_client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        response = await client.chat.completions.with_raw_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
            extra_headers={"x-stainless-retry-count": "42"},
        )

        assert response.http_request.headers.get("x-stainless-retry-count") == "42"

    @pytest.mark.parametrize("failures_before_success", [0, 2, 4])
    @mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
    @pytest.mark.respx2(base_url=base_url)
    async def test_retries_taken_new_response_class(
        self, async_client: AsyncOpenAI, failures_before_success: int, respx2_mock: MockRouter
    ) -> None:
        client = async_client.with_options(max_retries=4)

        nb_retries = 0

        def retry_handler(_request: httpx2.Request) -> httpx2.Response:
            nonlocal nb_retries
            if nb_retries < failures_before_success:
                nb_retries += 1
                return httpx2.Response(500)
            return httpx2.Response(200)

        respx2_mock.post("/chat/completions").mock(side_effect=retry_handler)

        async with client.chat.completions.with_streaming_response.create(
            messages=[
                {
                    "content": "string",
                    "role": "developer",
                }
            ],
            model="gpt-5.4",
        ) as response:
            assert response.retries_taken == failures_before_success
            assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

    async def test_get_platform(self) -> None:
        platform = await asyncify(get_platform)()
        assert isinstance(platform, (str, OtherPlatform))

    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 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()

        mounts = tuple(client._mounts.items())
        assert len(mounts) == 1
        assert mounts[0][0].pattern == "https://"

    @pytest.mark.filterwarnings("ignore:.*deprecated.*:DeprecationWarning")
    async def test_default_client_creation(self) -> None:
        # Ensure that the client can be initialized without any exceptions
        DefaultAsyncHttpxClient(
            verify=True,
            cert=None,
            trust_env=True,
            http1=True,
            http2=False,
            limits=httpx2.Limits(max_connections=100, max_keepalive_connections=20),
        )

    @pytest.mark.respx2(base_url=base_url)
    async def test_follow_redirects(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        # Test that the default follow_redirects=True allows following redirects
        respx2_mock.post("/redirect").mock(
            return_value=httpx2.Response(302, headers={"Location": f"{base_url}/redirected"})
        )
        respx2_mock.get("/redirected").mock(return_value=httpx2.Response(200, json={"status": "ok"}))

        response = await async_client.post("/redirect", body={"key": "value"}, cast_to=httpx2.Response)
        assert response.status_code == 200
        assert response.json() == {"status": "ok"}

    @pytest.mark.respx2(base_url=base_url)
    async def test_follow_redirects_disabled(self, respx2_mock: MockRouter, async_client: AsyncOpenAI) -> None:
        # Test that follow_redirects=False prevents following redirects
        respx2_mock.post("/redirect").mock(
            return_value=httpx2.Response(302, headers={"Location": f"{base_url}/redirected"})
        )

        with pytest.raises(APIStatusError) as exc_info:
            await async_client.post(
                "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx2.Response
            )

        assert exc_info.value.response.status_code == 302
        assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

    async def test_api_key_before_after_refresh_provider(self) -> None:
        async def mock_api_key_provider():
            return "test_bearer_token"

        client = AsyncOpenAI(base_url=base_url, api_key=mock_api_key_provider)

        assert client.api_key == ""
        assert "Authorization" not in client.auth_headers

        await client._refresh_api_key()

        assert client.api_key == "test_bearer_token"
        assert client.auth_headers.get("Authorization") == "Bearer test_bearer_token"

    async def test_api_key_before_after_refresh_str(self) -> None:
        client = AsyncOpenAI(base_url=base_url, api_key="test_api_key")

        assert client.auth_headers.get("Authorization") == "Bearer test_api_key"
        await client._refresh_api_key()

        assert client.auth_headers.get("Authorization") == "Bearer test_api_key"

    @pytest.mark.respx2()
    async def test_bearer_token_refresh_async(self, respx2_mock: MockRouter) -> None:
        respx2_mock.post(base_url + "/chat/completions").mock(
            side_effect=[
                httpx2.Response(500, json={"error": "server error"}),
                httpx2.Response(200, json={"foo": "bar"}),
            ]
        )

        counter = 0

        async def token_provider() -> str:
            nonlocal counter

            counter += 1

            if counter == 1:
                return "first"

            return "second"

        client = AsyncOpenAI(base_url=base_url, api_key=token_provider)
        await client.chat.completions.create(messages=[], model="gpt-4")

        calls = cast("list[MockRequestCall]", respx2_mock.calls)
        assert len(calls) == 2

        assert calls[0].request.headers.get("Authorization") == "Bearer first"
        assert calls[1].request.headers.get("Authorization") == "Bearer second"

    async def test_copy_auth(self) -> None:
        async def token_provider_1() -> str:
            return "test_bearer_token_1"

        async def token_provider_2() -> str:
            return "test_bearer_token_2"

        client = AsyncOpenAI(base_url=base_url, api_key=token_provider_1).copy(api_key=token_provider_2)
        await client._refresh_api_key()
        assert client.auth_headers == {"Authorization": "Bearer test_bearer_token_2"}


class TestWorkloadIdentity401Retry:
    @pytest.mark.respx2()
    def test_workload_identity_401_retry(self, respx2_mock: MockRouter) -> None:
        provider_call_count = 0

        def provider() -> str:
            nonlocal provider_call_count
            provider_call_count += 1
            return f"external-subject-token-{provider_call_count}"

        respx2_mock.post("https://auth.openai.com/oauth/token").mock(
            side_effect=[
                httpx2.Response(
                    200,
                    json={
                        "access_token": "openai-access-token-1",
                        "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                        "token_type": "Bearer",
                        "expires_in": 3600,
                    },
                ),
                httpx2.Response(
                    200,
                    json={
                        "access_token": "openai-access-token-2",
                        "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                        "token_type": "Bearer",
                        "expires_in": 3600,
                    },
                ),
            ]
        )

        respx2_mock.post(base_url + "/chat/completions").mock(
            side_effect=[
                httpx2.Response(401, json={"error": {"message": "Unauthorized", "type": "invalid_request_error"}}),
                httpx2.Response(
                    200,
                    json={
                        "id": "chatcmpl-123",
                        "object": "chat.completion",
                        "created": 1234567890,
                        "model": "gpt-4",
                        "choices": [],
                    },
                ),
            ]
        )

        with OpenAI(
            base_url=base_url,
            workload_identity={
                **workload_identity,
                "provider": {
                    "get_token": provider,
                    "token_type": "jwt",
                },
            },
            organization="org_123",
            project="proj_123",
            _strict_response_validation=True,
        ) as client:
            client.chat.completions.create(messages=[], model="gpt-4")

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 4

            assert calls[0].request.url == httpx2.URL("https://auth.openai.com/oauth/token")
            assert calls[1].request.url == httpx2.URL(base_url + "/chat/completions")
            assert calls[1].request.headers.get("Authorization") == "Bearer openai-access-token-1"

            assert calls[2].request.url == httpx2.URL("https://auth.openai.com/oauth/token")

            assert calls[3].request.url == httpx2.URL(base_url + "/chat/completions")
            assert calls[3].request.headers.get("Authorization") == "Bearer openai-access-token-2"

            assert provider_call_count == 2

    @pytest.mark.respx2()
    def test_401_without_workload_identity_no_retry(self, respx2_mock: MockRouter) -> None:
        respx2_mock.post(base_url + "/chat/completions").mock(
            return_value=httpx2.Response(
                401, json={"error": {"message": "Unauthorized", "type": "invalid_request_error"}}
            )
        )

        with OpenAI(
            base_url=base_url,
            api_key="test-api-key",
            _strict_response_validation=True,
        ) as client:
            with pytest.raises(APIStatusError) as exc_info:
                client.chat.completions.create(messages=[], model="gpt-4")

            assert exc_info.value.status_code == 401

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 1

    @pytest.mark.respx2()
    def test_non_401_errors_no_retry(self, respx2_mock: MockRouter) -> None:
        provider_call_count = 0

        def provider() -> str:
            nonlocal provider_call_count
            provider_call_count += 1
            return "external-subject-token"

        respx2_mock.post("https://auth.openai.com/oauth/token").mock(
            return_value=httpx2.Response(
                200,
                json={
                    "access_token": "openai-access-token-1",
                    "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                    "token_type": "Bearer",
                    "expires_in": 3600,
                },
            )
        )

        respx2_mock.post(base_url + "/chat/completions").mock(
            return_value=httpx2.Response(403, json={"error": {"message": "Forbidden", "type": "invalid_request_error"}})
        )

        with OpenAI(
            base_url=base_url,
            workload_identity={
                **workload_identity,
                "provider": {
                    "get_token": provider,
                    "token_type": "jwt",
                },
            },
            organization="org_123",
            project="proj_123",
            _strict_response_validation=True,
        ) as client:
            with pytest.raises(APIStatusError) as exc_info:
                client.chat.completions.create(messages=[], model="gpt-4")

            assert exc_info.value.status_code == 403

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 2

            assert provider_call_count == 1


class TestAsyncWorkloadIdentity401Retry:
    @pytest.mark.respx2()
    async def test_workload_identity_401_retry(self, respx2_mock: MockRouter) -> None:
        provider_call_count = 0

        def provider() -> str:
            nonlocal provider_call_count
            provider_call_count += 1
            return f"external-subject-token-{provider_call_count}"

        respx2_mock.post("https://auth.openai.com/oauth/token").mock(
            side_effect=[
                httpx2.Response(
                    200,
                    json={
                        "access_token": "openai-access-token-1",
                        "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                        "token_type": "Bearer",
                        "expires_in": 3600,
                    },
                ),
                httpx2.Response(
                    200,
                    json={
                        "access_token": "openai-access-token-2",
                        "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                        "token_type": "Bearer",
                        "expires_in": 3600,
                    },
                ),
            ]
        )

        respx2_mock.post(base_url + "/chat/completions").mock(
            side_effect=[
                httpx2.Response(401, json={"error": {"message": "Unauthorized", "type": "invalid_request_error"}}),
                httpx2.Response(
                    200,
                    json={
                        "id": "chatcmpl-123",
                        "object": "chat.completion",
                        "created": 1234567890,
                        "model": "gpt-4",
                        "choices": [],
                    },
                ),
            ]
        )

        async with AsyncOpenAI(
            base_url=base_url,
            workload_identity={
                **workload_identity,
                "provider": {
                    "get_token": provider,
                    "token_type": "jwt",
                },
            },
            organization="org_123",
            project="proj_123",
            _strict_response_validation=True,
        ) as client:
            await client.chat.completions.create(messages=[], model="gpt-4")

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 4

            assert calls[0].request.url == httpx2.URL("https://auth.openai.com/oauth/token")
            assert calls[1].request.url == httpx2.URL(base_url + "/chat/completions")
            assert calls[1].request.headers.get("Authorization") == "Bearer openai-access-token-1"

            assert calls[2].request.url == httpx2.URL("https://auth.openai.com/oauth/token")

            assert calls[3].request.url == httpx2.URL(base_url + "/chat/completions")
            assert calls[3].request.headers.get("Authorization") == "Bearer openai-access-token-2"

            assert provider_call_count == 2

    @pytest.mark.respx2()
    async def test_401_without_workload_identity_no_retry(self, respx2_mock: MockRouter) -> None:
        respx2_mock.post(base_url + "/chat/completions").mock(
            return_value=httpx2.Response(
                401, json={"error": {"message": "Unauthorized", "type": "invalid_request_error"}}
            )
        )

        async with AsyncOpenAI(
            base_url=base_url,
            api_key="test-api-key",
            _strict_response_validation=True,
        ) as client:
            with pytest.raises(APIStatusError) as exc_info:
                await client.chat.completions.create(messages=[], model="gpt-4")

            assert exc_info.value.status_code == 401

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 1

    @pytest.mark.respx2()
    async def test_non_401_errors_no_retry(self, respx2_mock: MockRouter) -> None:
        provider_call_count = 0

        def provider() -> str:
            nonlocal provider_call_count
            provider_call_count += 1
            return "external-subject-token"

        respx2_mock.post("https://auth.openai.com/oauth/token").mock(
            return_value=httpx2.Response(
                200,
                json={
                    "access_token": "openai-access-token-1",
                    "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
                    "token_type": "Bearer",
                    "expires_in": 3600,
                },
            )
        )

        respx2_mock.post(base_url + "/chat/completions").mock(
            return_value=httpx2.Response(403, json={"error": {"message": "Forbidden", "type": "invalid_request_error"}})
        )

        async with AsyncOpenAI(
            base_url=base_url,
            workload_identity={
                **workload_identity,
                "provider": {
                    "get_token": provider,
                    "token_type": "jwt",
                },
            },
            organization="org_123",
            project="proj_123",
            _strict_response_validation=True,
        ) as client:
            with pytest.raises(APIStatusError) as exc_info:
                await client.chat.completions.create(messages=[], model="gpt-4")

            assert exc_info.value.status_code == 403

            calls = cast("list[MockRequestCall]", respx2_mock.calls)
            assert len(calls) == 2

            assert provider_call_count == 1
