{"record":{"id":"8e6f81c6a272cd3f","repo":"openai/openai-python","slug":"invalid-http-client-argument-expected-an-instan-8e6f81","errorCode":null,"errorMessage":"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` or `httpx2.AsyncClient` but got {type(http_client)}","messagePattern":"Invalid `http_client` argument; Expected an instance of `httpx\\.AsyncClient` or `httpx2\\.AsyncClient` but got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/openai/_base_client.py","lineNumber":1551,"sourceCode":"            # if the user passed in a custom http client with a non-default\n            # timeout set then we use that timeout.\n            #\n            # note: there is an edge case here where the user passes in a client\n            # where they've explicitly set the timeout to match the default timeout\n            # as this check is structural, meaning that we'll think they didn't\n            # pass in a timeout and will ignore it\n            client_timeout = normalize_httpx_timeout(http_client.timeout) if http_client else None\n            if http_client and client_timeout != HTTPX_DEFAULT_TIMEOUT:\n                timeout = client_timeout\n            else:\n                timeout = DEFAULT_TIMEOUT\n\n        if (\n            http_client is not None\n            and not is_httpx2_async_client(http_client)\n            and not is_legacy_httpx_async_client(http_client)\n        ):\n            raise TypeError(\n                \"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` or \"\n                f\"`httpx2.AsyncClient` but got {type(http_client)}\"\n            )\n\n        super().__init__(\n            version=version,\n            base_url=base_url,\n            # cast to a valid type because mypy doesn't understand our type narrowing\n            timeout=cast(Timeout, timeout),\n            max_retries=max_retries,\n            custom_query=custom_query,\n            custom_headers=custom_headers,\n            _strict_response_validation=_strict_response_validation,\n        )\n        self._client = http_client or AsyncHttpxClientWrapper(\n            base_url=base_url,\n            # cast to a valid type because mypy doesn't understand our type narrowing\n            timeout=cast(Timeout, timeout),","sourceCodeStart":1533,"sourceCodeEnd":1569,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_base_client.py#L1533-L1569","documentation":"The async client validates that a user-supplied `http_client` is an `httpx.AsyncClient` (legacy) or `httpx2.AsyncClient`. Any other object — including a sync `httpx.Client`, a wrapper, or a mock — is rejected with this TypeError at construction time.","triggerScenarios":"Passing a sync `httpx.Client` to `AsyncOpenAI(http_client=...)`; passing a custom wrapper/protocol object; passing an `httpx2.Client` (sync) or a mocked client that lacks the expected base classes.","commonSituations":"Copy-pasting sync-client setup into async code; test doubles/fakes that subclass nothing; version drift after the SDK moved to httpx2 while user code still passes old-style clients of the wrong type.","solutions":["Pass an async client instance: `httpx2.AsyncClient(...)` or `httpx.AsyncClient(...)`","For sync `OpenAI`, pass `httpx.Client`/`httpx2.Client` instead","In tests, subclass `httpx2.AsyncClient` or use a mock that passes `isinstance` checks"],"exampleFix":"# before\nclient = AsyncOpenAI(http_client=httpx2.Client(...))  # sync!\n\n# after\nclient = AsyncOpenAI(http_client=httpx2.AsyncClient(...))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import httpx, httpx2\n\ndef is_acceptable_async_client(c: object) -> bool:\n    return isinstance(c, (httpx.AsyncClient, httpx2.AsyncClient))\n\nassert is_acceptable_async_client(http_client), 'http_client must be an async httpx client'","tryCatchPattern":"try:\n    client = AsyncOpenAI(http_client=http_client)\nexcept TypeError as e:\n    if 'http_client' in str(e):\n        raise ValueError('Provide an httpx.AsyncClient/httpx2.AsyncClient instance') from e\n    raise","preventionTips":["Name sync and async clients distinctly (http_client vs async_http_client) to avoid mix-ups","Lint for `httpx2.Client` being passed where an async client is expected in async functions"],"tags":["http-client","typeerror","async","validation","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}