Skip to content

Pydantic serializer warnings when dumping a completion response with logprobs #744

Closed
@kddubey

Description

@kddubey

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

  • This is an issue with the Python library

Describe the bug

Dumping a completion response from a request with logprobs set raises this warning:

python3.11/site-packages/pydantic/main.py:308: UserWarning: Pydantic serializer warnings:
  Expected `int` but got `float` - serialized value may not be as expected
  Expected `int` but got `float` - serialized value may not be as expected
  Expected `int` but got `float` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(

I confirmed that changing this type annotation to

    top_logprobs: Optional[List[Dict[str, float]]] = None

removes the warnings.

To Reproduce

  1. Install openai>=1.0.0
  2. In a completions API call, set logprobs>=1
  3. Dump the response using .model_dump(), as advised in the migration guide

Code snippets

import openai

client = openai.OpenAI()

# Set logprobs >= 1
response = client.completions.create(
    model="curie", prompt="test", max_tokens=3, logprobs=1
)

response_as_dict = response.model_dump()
# raises a UserWarning

To ignore the warnings, the user currently has to do this:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings(
        "ignore",
        category=UserWarning,
        message="Pydantic serializer warnings",
    )
    response_as_dict = response.model_dump()

OS

macOS

Python version

Python v3.11.5

Library version

openai v1.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions