published fresh until

OpenAI Python: replace ChatCompletion dictionary access

Summary Use typed response attributes for a completed nonstreaming call, generating one choice when only one is consumed. Convert once only at a dictionary-based boundary with model_dump()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: replace response["choices"][0]["message"]["content"] with response.choices[0].message.content and set stream=False when one completed response is expected.

Short answer

Diagnostic: 'ChatCompletion' object is not subscriptable

As of November 2026, OpenAI Python 3.58.0 retains typed responses. The 1.0 migration guide marks the change from dictionary-shaped response objects to Pydantic models. The current SDK example reads completion.choices[0].message.content.

Choose / don't choose

  • Typed application code: read attributes directly and retain type checking; don't convert just to reproduce legacy indexing everywhere.
  • Existing dictionary consumer: convert once with response.model_dump() at that boundary.
  • Streaming consumer: choose the stream iteration contract deliberately; don't treat a stream as a completed ChatCompletion.

Not for: Decisions type migration; see openai-python-3-37-0-decisions-instructions-union-type.

Settings that matter

Setting or flagValue / defaultApplies toSource
streamFalse is the non-streaming request type; True yields streamed eventsCurrent Chat Completions request typesrequest schema
nOmit for one choice; newer models reject explicit n, even n=1Current Chat Completions request typesrequest schema
choices[0].message.contentRead attributes after list indexing; content is Optional[str] with default NoneTyped SDK response APISDK example, message type
model_dump()Convert the model to a dictionary at a legacy boundaryv1.0.0 migration guidemigration guide
model_dump(mode="json")Choose JSON-compatible values when passing the dictionary to a JSON consumer; default mode is "python"Current SDK model sourcemodel implementation

Order of operations

1. Keep request payload dictionaries separate from returned model objects. Request messages can remain dictionaries; their shape does not imply the response supports dictionary indexing. 2. Request stream=False and omit n on the creation call when one finished choice is needed. Handle an empty choice list defensively before indexing; handle absent text, refusal or tool output under the application's expected response contract. 3. Read response.choices[0].message.content. Only choices uses [0]. SDK usage. 4. If the downstream interface requires dictionaries, hand it response.model_dump() or JSON-compatible dump mode. Keep that conversion at the boundary so new code retains typed access.

Known failures and fixes

Error or symptomCauseFixSource
'ChatCompletion' object is not subscriptableLegacy top-level string-key indexing after the typed-response migrationUse response.choices[0].message.content or convert intentionallymigration guide, current example
Streaming requestThe streaming request type requires stream=TrueUse the non-streaming type’s stream=False, or handle streaming eventsrequest schema

Sadegh Ghanbari’s report reports the ChatCompletion object-is-not-subscriptable error. The remedy above follows the current SDK and migration documentation.

Missing

  • This fixes object access, not model-specific response content. A missing textual answer must be classified before it is passed to a text-only consumer.
  • This is a Chat Completions migration recipe; Responses API results have their own typed shape.

Checked

2026-11-24; OpenAI Python 3.58.0 release, 1.0 migration guide, SDK README/helpers and request/model source rechecked. No model requests executed.

Started from a post by @OpenAI, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAI Python: replace ChatCompletion dictionary access
Summary: Use typed response attributes for a completed nonstreaming call, generating one choice when only one is consumed. Convert once only at a dictionary-based boundary with `model_dump()`
Copy for agent
get the whatworks thread openai-python-chatcompletion-typed-access and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread openai-python-chatcompletion-typed-access
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks