Skip to content

Commit 182b763

Browse files
fix(client): don't send Content-Type header on GET requests
1 parent db5c350 commit 182b763

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Repository = "https://github.com/openai/openai-python"
4444
openai = "openai.cli:main"
4545

4646
[project.optional-dependencies]
47-
aiohttp = ["aiohttp", "httpx_aiohttp>=0.1.6"]
47+
aiohttp = ["aiohttp", "httpx_aiohttp>=0.1.8"]
4848
realtime = ["websockets >= 13, < 16"]
4949
datalib = ["numpy >= 1", "pandas >= 1.2.3", "pandas-stubs >= 1.1.0.11"]
5050
voice_helpers = ["sounddevice>=0.5.1", "numpy>=2.0.2"]

src/openai/_base_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,15 @@ def _build_request(
531531
# work around https://github.com/encode/httpx/discussions/2880
532532
kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")}
533533

534+
is_body_allowed = options.method.lower() != "get"
535+
536+
if is_body_allowed:
537+
kwargs["json"] = json_data if is_given(json_data) else None
538+
kwargs["files"] = files
539+
else:
540+
headers.pop("Content-Type", None)
541+
kwargs.pop("data", None)
542+
534543
# TODO: report this error to httpx
535544
return self._client.build_request( # pyright: ignore[reportUnknownMemberType]
536545
headers=headers,
@@ -542,8 +551,6 @@ def _build_request(
542551
# so that passing a `TypedDict` doesn't cause an error.
543552
# https://github.com/microsoft/pyright/issues/3526#event-6715453066
544553
params=self.qs.stringify(cast(Mapping[str, Any], params)) if params else None,
545-
json=json_data if is_given(json_data) else None,
546-
files=files,
547554
**kwargs,
548555
)
549556

tests/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def test_request_extra_query(self) -> None:
463463
def test_multipart_repeating_array(self, client: OpenAI) -> None:
464464
request = client._build_request(
465465
FinalRequestOptions.construct(
466-
method="get",
466+
method="post",
467467
url="/foo",
468468
headers={"Content-Type": "multipart/form-data; boundary=6b7ba517decee4a450543ea6ae821c82"},
469469
json_data={"array": ["foo", "bar"]},
@@ -1348,7 +1348,7 @@ def test_request_extra_query(self) -> None:
13481348
def test_multipart_repeating_array(self, async_client: AsyncOpenAI) -> None:
13491349
request = async_client._build_request(
13501350
FinalRequestOptions.construct(
1351-
method="get",
1351+
method="post",
13521352
url="/foo",
13531353
headers={"Content-Type": "multipart/form-data; boundary=6b7ba517decee4a450543ea6ae821c82"},
13541354
json_data={"array": ["foo", "bar"]},

0 commit comments

Comments
 (0)