-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Open
Labels
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