fix(chatgpt): keep session typing and test diff scoped

This commit is contained in:
Onur Cakmak 2026-10-02 12:42:18 -04:00
parent ead8edc422
commit 84ed22e102
2 changed files with 33 additions and 13 deletions

View file

@ -252,7 +252,7 @@ def get_chatgpt_default_instructions() -> str:
return os.getenv("CHATGPT_DEFAULT_INSTRUCTIONS") or CHATGPT_DEFAULT_INSTRUCTIONS
def _normalize_litellm_params(litellm_params: Any | None) -> dict:
def _normalize_litellm_params(litellm_params: Any | None) -> dict[str, object]:
if litellm_params is None:
return {}
if isinstance(litellm_params, dict):
@ -274,8 +274,9 @@ def get_chatgpt_session_id(litellm_params: object) -> str | None:
params: Final = _normalize_litellm_params(litellm_params)
metadata: Final = params.get("metadata")
generated: Final = any(
isinstance(params.get(name), dict) and params[name].get(SESSION_ID_GENERATED_METADATA_KEY)
for name in ("metadata", "litellm_metadata")
True
for session_metadata in (metadata, params.get("litellm_metadata"))
if isinstance(session_metadata, dict) and session_metadata.get(SESSION_ID_GENERATED_METADATA_KEY)
)
if not generated:
for key in ("litellm_session_id", "session_id"):
@ -286,7 +287,7 @@ def get_chatgpt_session_id(litellm_params: object) -> str | None:
value = metadata.get("session_id")
if value:
return str(value)
prompt_cache_key: Final = params.get("prompt_cache_key")
prompt_cache_key: Final[object] = params.get("prompt_cache_key")
if prompt_cache_key:
key = str(prompt_cache_key)
safe = _safe_header_value(key)

View file

@ -60,6 +60,7 @@ class TestChatGPTResponsesAPITransformation:
assert isinstance(config, ChatGPTResponsesAPIConfig)
assert config.custom_llm_provider == LlmProviders.CHATGPT
@pytest.mark.parametrize(
"model_name",
[
@ -96,10 +97,14 @@ class TestChatGPTResponsesAPITransformation:
url = config.get_complete_url(api_base=None, litellm_params={})
assert url == "https://chatgpt.example.com/responses"
custom_url = config.get_complete_url(api_base="https://custom.chatgpt.com", litellm_params={})
custom_url = config.get_complete_url(
api_base="https://custom.chatgpt.com", litellm_params={}
)
assert custom_url == "https://custom.chatgpt.com/responses"
url_with_slash = config.get_complete_url(api_base="https://chatgpt.example.com/", litellm_params={})
url_with_slash = config.get_complete_url(
api_base="https://chatgpt.example.com/", litellm_params={}
)
assert url_with_slash == "https://chatgpt.example.com/responses"
@patch("litellm.llms.chatgpt.responses.transformation.Authenticator")
@ -162,7 +167,9 @@ class TestChatGPTResponsesAPITransformation:
"user": "user_123",
"temperature": 0.2,
"top_p": 0.9,
"context_management": [{"type": "compaction", "compact_threshold": 200000}],
"context_management": [
{"type": "compaction", "compact_threshold": 200000}
],
"metadata": {"foo": "bar"},
"max_output_tokens": 123,
"stream_options": {"include_usage": True},
@ -201,7 +208,9 @@ class TestChatGPTResponsesAPITransformation:
("chatgpt/gpt-5.3-codex", "gpt-5.3-codex"),
],
)
def test_chatgpt_non_stream_sse_response_parsing(self, model_name: str, response_model: str):
def test_chatgpt_non_stream_sse_response_parsing(
self, model_name: str, response_model: str
):
config = ChatGPTResponsesAPIConfig()
response_payload = {
"id": "resp_test",
@ -224,7 +233,9 @@ class TestChatGPTResponsesAPITransformation:
"",
]
)
raw_response = httpx.Response(200, headers={"content-type": "text/event-stream"}, text=sse_body)
raw_response = httpx.Response(
200, headers={"content-type": "text/event-stream"}, text=sse_body
)
logging_obj = MagicMock()
parsed = config.transform_response_api_response(
@ -242,7 +253,9 @@ class TestChatGPTResponsesAPITransformation:
("chatgpt/gpt-5.3-codex", "gpt-5.3-codex"),
],
)
def test_chatgpt_non_stream_sse_response_recovers_output_items(self, model_name: str, response_model: str):
def test_chatgpt_non_stream_sse_response_recovers_output_items(
self, model_name: str, response_model: str
):
config = ChatGPTResponsesAPIConfig()
response_payload = {
"id": "resp_test",
@ -265,7 +278,9 @@ class TestChatGPTResponsesAPITransformation:
"",
]
)
raw_response = httpx.Response(200, headers={"content-type": "text/event-stream"}, text=sse_body)
raw_response = httpx.Response(
200, headers={"content-type": "text/event-stream"}, text=sse_body
)
logging_obj = MagicMock()
parsed = config.transform_response_api_response(
@ -305,7 +320,9 @@ class TestChatGPTResponsesAPITransformation:
"",
]
)
raw_response = httpx.Response(200, headers={"content-type": "text/event-stream"}, text=sse_body)
raw_response = httpx.Response(
200, headers={"content-type": "text/event-stream"}, text=sse_body
)
logging_obj = MagicMock()
parsed = config.transform_response_api_response(
@ -338,7 +355,9 @@ class TestChatGPTResponsesAPITransformation:
"",
]
)
raw_response = httpx.Response(502, headers={"content-type": "text/event-stream"}, text=sse_body)
raw_response = httpx.Response(
502, headers={"content-type": "text/event-stream"}, text=sse_body
)
logging_obj = MagicMock()
with pytest.raises(OpenAIError) as exc_info: