openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.96k stars 3.22k forks source link

limit parameter in messages.list does not work. #1863

Closed hkaraoguz closed 1 day ago

hkaraoguz commented 1 day ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

When I try to fetch a limited number of messages from a thread, I receive all the messages instead.

To Reproduce

  1. Fetch a thread
  2. Retrieve the messages of the thread with limit parameter
  3. Iterate over retrieved message objects

Code snippets

import openai
import os

client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))

thread = client.beta.threads.retrieve(thread_id="<thread_id>")

messages  = client.beta.threads.messages.list(thread_id=thread.id, limit=3)

for i,message in enumerate(messages):
    print(message.role)
    print(message.created_at)

OS

Ubuntu 22.04

Python version

Python v3.10.12

Library version

openai v1.54.3

RobertCraigie commented 1 day ago

I can see how this isn't intuitive but this is happening because the iterator is making multiple requests, if you just want the single page then you need to use .data

import openai
import os

client = openai.Client(api_key=os.getenv("OPENAI_API_KEY"))

thread = client.beta.threads.retrieve(thread_id="<thread_id>")

messages  = client.beta.threads.messages.list(thread_id=thread.id, limit=3)

for i,message in enumerate(messages.data):
    print(message.role)
    print(message.created_at)

Unfortunately I'm not sure if there's anything we can change to make this easier to understand...

hkaraoguz commented 1 day ago

Thank you @RobertCraigie for your prompt reply. I suspected that I was misunderstanding the usage and now I learned.