openai/openai-python · error · ValueError
Expected a non-empty value for `session_id` but received {se
Error message
Expected a non-empty value for `session_id` but received {session_id!r} What it means
The sync ChatKit sessions cancel method requires a non-empty `session_id` for the URL `/chatkit/sessions/{session_id}/cancel`. The SDK raises ValueError locally when the value is falsy so no malformed request is sent.
Source
Thrown at src/openai/resources/beta/chatkit/sessions.py:138
extra_body: Body | None = None,
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
) -> ChatSession:
"""
Cancel an active ChatKit session and return its most recent metadata.
Cancelling prevents new requests from using the issued client secret.
Args:
extra_headers: Send extra headers
extra_query: Add additional query parameters to the request
extra_body: Add additional JSON properties to the request
timeout: Override the client-level default timeout for this request, in seconds
"""
if not session_id:
raise ValueError(f"Expected a non-empty value for `session_id` but received {session_id!r}")
extra_headers = {"OpenAI-Beta": "chatkit_beta=v1", **(extra_headers or {})}
return self._post(
path_template("/chatkit/sessions/{session_id}/cancel", session_id=session_id),
options=make_request_options(
extra_headers=extra_headers,
extra_query=extra_query,
extra_body=extra_body,
timeout=timeout,
security={"bearer_auth": True},
),
cast_to=ChatSession,
)
class AsyncSessions(AsyncAPIResource):
@cached_property
def with_raw_response(self) -> AsyncSessionsWithRawResponse:
"""View on GitHub (pinned to 9917c6e28e)
Solutions
- Capture and pass the session_id returned when the session was created
- Check the variable is a non-empty string before cancelling
- Log session IDs at creation time for tracing
Example fix
// before client.beta.chatkit.sessions.cancel(session_id=sid) # sid accidentally "" // after assert sid, "session_id must come from create()" client.beta.chatkit.sessions.cancel(session_id=sid)
Defensive patterns
Strategy: validation
Validate before calling
if not session_id:
raise ValueError("session_id is required to cancel a ChatKit session") Type guard
def is_valid_session_id(v: object) -> bool:
return isinstance(v, str) and bool(v.strip()) Try / catch
try:
client.beta.chatkit.sessions.cancel(session_id=sid)
except ValueError as e:
logger.warning("cannot cancel session: %s", e) Prevention
- Store session IDs immediately after create
- Use distinct names for session_id and thread_id
When it happens
Trigger: Calling `client.beta.chatkit.sessions.cancel(session_id="")` or None on the sync client (sessions.py:138).
Common situations: Cancelling a session whose ID was never captured from the create response, or a variable naming mix-up between session/thread IDs.
Related errors
- Expected a non-empty value for `thread_id` but received {thr
- Expected a non-empty value for `project_id` but received {pr
- Expected a non-empty value for `project_id` but received {pr
- Expected a non-empty value for `project_id` but received {pr
- Expected a non-empty value for `group_id` but received {grou
AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28).
Data as JSON: /api/errors/97a35d688128c5fd.
Report an issue: GitHub.