openai / openai/openai-python

Encountering a 500 error when calling responses.parse on a schema containing decimal.Decimal fields.

Open
#2,718 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api bug
Dominant language
Python
Stars
31.6k
Forks
5.7k
Avg merge
1d 6h
Merged PRs (30d)
96

Description

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

The responses.parse() method fails when the Pydantic model contains decimal.Decimal type fields.

To Reproduce

Run this test script below


import logging
from decimal import Decimal
from typing import Optional
from pydantic import BaseModel, Field
from openai import OpenAI

# Set up logger
logging.basicConfig(level=logging.INFO, format='%(message)s')
logger = logging.getLogger(__name__)

# Define test models
class DecimalModel(BaseModel):
    """Model with Decimal fields."""
    premium: Optional[Decimal] = Field(default=None, description="Premium amount")
    amount: Optional[Decimal] = Field(default=None, description="Total amount")

class FloatModel(BaseModel):
    """Model with float fields."""
    premium: Optional[float] = Field(default=None, description="Premium amount")
    amount: Optional[float] = Field(default=None, description="Total amount")

class StringModel(BaseModel):
    """Model with string fields."""
    premium: Optional[str] = Field(default=None, description="Premium amount")
    amount: Optional[str] = Field(default=None, description="Total amount")

# Initialize client and test data
client = OpenAI()
test_content = "The premium is $5,000.00 and the total amount is $5,500.50"

request_model = "gpt-5"
# request_model = "gpt-5-mini" # Uncomment to test with a mini
# Test String Model
try:
    response = client.responses.parse(
        model=request_model,
        input=[
            {"role": "system", "content": "Extract the financial amounts."},
            {"role": "user", "content": test_content}
        ],
        text_format=StringModel,
    )
    logger.info(f"StringModel: WORKS. Data: {response.output_parsed.model_dump_json()}")
except Exception as e:
    logger.error(f"StringModel: FAILED - {type(e).__name__}")
    logger.exception(e)

# Test Float Model
try:
    response = client.responses.parse(
        model=request_model,
        input=[
            {"role": "system", "content": "Extract the financial amounts."},
            {"role": "user", "content": test_content}
        ],
        text_format=FloatModel,
    )
    logger.info(f"FloatModel: WORKS. Data: {response.output_parsed.model_dump_json()}")
except Exception as e:
    logger.error(f"FloatModel: FAILED - {type(e).__name__}")
    logger.exception(e)

# Test Decimal Model
try:
    response = client.responses.parse(
        model=request_model,
        input=[
            {"role": "system", "content": "Extract the financial amounts."},
            {"role": "user", "content": test_content}
        ],
        text_format=DecimalModel,
    )
    logger.info(f"DecimalModel: WORKS. Data: {response.output_parsed.model_dump_json()}")
except Exception as e:
    logger.error(f"DecimalModel: FAILED - {type(e).__name__}")
    logger.exception(e)

Output with gpt-5

> python test_decimal_extraction.py
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 200 OK"
StringModel: WORKS. Data: {"premium":"$5,000.00","amount":"$5,500.50"}
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 200 OK"
FloatModel: WORKS. Data: {"premium":5000.0,"amount":5500.5}
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 500 Internal Server Error"
Retrying request to /responses in 0.459931 seconds
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 500 Internal Server Error"
Retrying request to /responses in 0.893894 seconds
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 500 Internal Server Error"
DecimalModel: FAILED - InternalServerError
Error code: 500 - {'error': {'message': 'An error occurred while processing your request. You can retry your request, or contact us through our help center at help.openai.com if the error persists. Please include the request ID req_2e82ffe03e6d45f38ad7ff9b98a62ec4 in your message.', 'type': 'server_error', 'param': None, 'code': 'server_error'}}

Works as expected with gpt-5-mini

> python test_decimal_extraction.py
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 200 OK"
StringModel: WORKS. Data: {"premium":"$5,000.00","amount":"$5,500.50"}
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 200 OK"
FloatModel: WORKS. Data: {"premium":5000.0,"amount":5500.5}
HTTP Request: POST https://api.openai.com/v1/responses "HTTP/1.1 200 OK"
DecimalModel: WORKS. Data: {"premium":"5000.00","amount":"5500.50"}
Code snippets

OS

Debian GNU/Linux

Python version

3.12.10

Library version

openai v1.109.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided script and compare responses.parse with StringModel, FloatModel, and DecimalModel, especially the Pydantic schema generated for Decimal fields. Trace the library's handling of that schema and add a regression test; done means DecimalModel succeeds with gpt-5 without the reported 500 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.