Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
if not base_url:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not default explicit empty base_url values

Using if not base_url here changes semantics for explicitly provided values: OpenAI(base_url="") (and the async constructor’s equivalent branch) now silently targets https://api.openai.com/v1 instead of surfacing an invalid configuration. This can turn a caller/config bug into unintended traffic to production, which is harder to detect than a startup failure. The fallback should apply only when base_url is missing (None/unset env), not when the caller passed an explicit empty string.

Useful? React with 👍 / 👎.

base_url = f"https://api.openai.com/v1"

super().__init__(
Expand Down Expand Up @@ -537,7 +537,7 @@ def __init__(

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
if not base_url:
base_url = f"https://api.openai.com/v1"

super().__init__(
Expand Down
2 changes: 1 addition & 1 deletion src/openai/types/responses/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def output_text(self) -> str:
for output in self.output:
if output.type == "message":
for content in output.content:
if content.type == "output_text":
if content.type == "output_text" and content.text is not None:
texts.append(content.text)

return "".join(texts)
61 changes: 61 additions & 0 deletions tests/lib/responses/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from openai import OpenAI, AsyncOpenAI
from openai._utils import assert_signatures_in_sync
from openai.types.responses.response import Response

from ...conftest import base_url
from ..snapshots import make_snapshot_request
Expand Down Expand Up @@ -41,6 +42,66 @@ def test_output_text(client: OpenAI, respx_mock: MockRouter) -> None:
)


def test_output_text_with_null_text() -> None:
"""Test that output_text handles null text values without raising TypeError.

Regression test for https://github.com/openai/openai-python/issues/3011.
Some models return output_text content items where the text field is null.
"""
response = Response.construct(
id="resp_test",
object="response",
created_at=1700000000.0,
model="gpt-4o",
output=[
{
"id": "msg_test",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "annotations": [], "text": None},
{"type": "output_text", "annotations": [], "text": "Hello"},
],
}
],
parallel_tool_calls=True,
tool_choice="auto",
tools=[],
status="completed",
)

# Should not raise TypeError and should skip null text values
assert response.output_text == "Hello"


def test_output_text_all_null_text() -> None:
"""Test that output_text returns empty string when all text values are null."""
response = Response.construct(
id="resp_test",
object="response",
created_at=1700000000.0,
model="gpt-4o",
output=[
{
"id": "msg_test",
"type": "message",
"status": "completed",
"role": "assistant",
"content": [
{"type": "output_text", "annotations": [], "text": None},
],
}
],
parallel_tool_calls=True,
tool_choice="auto",
tools=[],
status="completed",
)

assert response.output_text == ""


@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
def test_stream_method_definition_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None:
checking_client: OpenAI | AsyncOpenAI = client if sync else async_client
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,11 @@ def test_base_url_env(self) -> None:
client = OpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

def test_empty_base_url_env_falls_back_to_default(self) -> None:
with update_env(OPENAI_BASE_URL=""):
client = OpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "https://api.openai.com/v1/"

@pytest.mark.parametrize(
"client",
[
Expand Down Expand Up @@ -1734,6 +1739,11 @@ async def test_base_url_env(self) -> None:
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

async def test_empty_base_url_env_falls_back_to_default(self) -> None:
with update_env(OPENAI_BASE_URL=""):
client = AsyncOpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "https://api.openai.com/v1/"

@pytest.mark.parametrize(
"client",
[
Expand Down