openai/openai-python · error · ValueError

Expected a non-empty value for `thread_id` but received {thr

Error message

Expected a non-empty value for `thread_id` but received {thread_id!r}

What it means

`client.beta.threads.runs.steps.retrieve(thread_id, run_id, step_id)` requires a non-empty `thread_id` as the first of three validated path parameters for `/threads/{thread_id}/runs/{run_id}/steps/{step_id}`. A falsy value raises this ValueError client-side with no HTTP request.

Source

Thrown at src/openai/resources/beta/threads/runs/steps.py:84

        Args:
          include: A list of additional fields to include in the response. Currently the only
              supported value is `step_details.tool_calls[*].file_search.results[*].content`
              to fetch the file search result content.

              See the
              [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
              for more information.

          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 thread_id:
            raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        if not step_id:
            raise ValueError(f"Expected a non-empty value for `step_id` but received {step_id!r}")
        extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
        return self._get(
            path_template(
                "/threads/{thread_id}/runs/{run_id}/steps/{step_id}",
                thread_id=thread_id,
                run_id=run_id,
                step_id=step_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform({"include": include}, step_retrieve_params.StepRetrieveParams),

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Keep the full (thread_id, run_id, step_id) triple together when storing step references
  2. Validate all three IDs before calling retrieve
  3. Re-derive missing IDs from the run/thread you originally listed

Example fix

# before
step = client.beta.threads.runs.steps.retrieve(thread_id=None, run_id=run_id, step_id=step_id)

# after
step = client.beta.threads.runs.steps.retrieve(thread_id=thread.id, run_id=run.id, step_id=step_id)
Defensive patterns

Strategy: validation

Validate before calling

if not (thread_id and run_id and step_id):
    raise ValueError(f"need thread/run/step ids, got {thread_id!r} {run_id!r} {step_id!r}")
step = client.beta.threads.runs.steps.retrieve(thread_id=thread_id, run_id=run_id, step_id=step_id)

Type guard

def is_step_ref(triple: tuple) -> TypeGuard[tuple[str, str, str]]:
    t, r, s = triple
    return all(isinstance(x, str) and x for x in (t, r, s))

Try / catch

try:
    step = client.beta.threads.runs.steps.retrieve(thread_id=thread_id, run_id=run_id, step_id=step_id)
except ValueError as e:
    logger.error("invalid step reference: %s", e)
    raise

Prevention

When it happens

Trigger: Fetching a run step detail while the thread ID variable is None/empty — often when iterating steps from a listing but constructing the retrieve call with an incomplete context object.

Common situations: Storing step references without their parent thread/run IDs, or copying example code and forgetting to substitute the thread ID.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


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