Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
AI and LLM integration

TypeError when you index an OpenAI response like a dictionary

ErrorTypeError: 'ChatCompletion' object is not subscriptable

Short answer

Since SDK 1.0 the OpenAI Python library returns Pydantic model objects instead of dictionaries, so response['choices'] fails. Read fields as attributes, for example completion.choices[0].message.content. If another library needs a real dict, convert the object with model_dump() or to_dict() first.

  • Applies to openai Python SDK 1.x and later, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the type of what came back and the names it carries. If it says ChatCompletion, you hold an object and square brackets will never work on it.

    r = client.chat.completions.create(model=MODEL_NAME, messages=messages)
    print(type(r))
    print(r.model_dump().keys())
  2. 2Match what you see, then jump to its fix

    1. 1The failing line looks like response['choices'][0]['message']['content'] or response.get('usage').Dictionary-style access from the old SDKFix
    2. 2json.dumps(response) or a logging, caching or database layer fails with a TypeError about the response type.A function that expects a plain dict gets the objectFix

What does the error look like?

  • TypeError: 'ChatCompletion' object is not subscriptable

Cause 1 of 2

Dictionary-style access from the old SDK

1Check

Find every place that reads the response with brackets or .get().

grep -rnE "\[['\"]choices['\"]\]|\.get\(['\"]usage" --include='*.py' .

2Fix

Use attribute access. The v1 guide shows the same rewrite for completions: brackets become dots, and .get('usage') becomes a lookup on a dict built from the object.

text = completion.choices[0].message.content
usage = dict(completion).get("usage")

3Verify

The text prints and no TypeError is raised.

print(text)

Confirmed by github.com

Did this fix it?

Cause 2 of 2

A function that expects a plain dict gets the object

1Check

Look for the place where the whole response object is handed to code that serialises it.

grep -rn "json.dumps(" --include='*.py' .

2Fix

Convert once at the boundary. model_dump() returns a dict, model_dump_json() returns a JSON string, and to_dict() and to_json() are documented helpers on the same objects.

data = completion.model_dump()
as_json = completion.model_dump_json(indent=2)

3Verify

Store or print data and confirm it is plain Python types.

print(type(data))

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Wrap the SDK call in one small function that returns just the text you need, so brackets never spread through the code.
  • Add a test that asserts on completion.choices[0].message.content so an SDK change fails loudly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.