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

  1. Capture and pass the session_id returned when the session was created
  2. Check the variable is a non-empty string before cancelling
  3. 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

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


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/97a35d688128c5fd. Report an issue: GitHub.