Usage of pydantic model with pattern throws error as it is not supported
I want the values of my pydantic models attribute to be in a specific format, is it possible?
i know we can place the regex in prompt and instruct it to follow, but it wont be reliable as the structured output feature (since it uses Constrained Decoding)
import openai
from openai import OpenAI
class CodeBlock(BaseModel):
loop:str = Field(pattern="for\(.+;.+;.+\)")
prompt = """
Convert the following into a cpp code snippet:
Steps
Start with 3, increment by 2, and end at 10
"""
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": prompt
},
],
response_format=CodeBlock
)
Error
BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'CodeBlock': In context=('properties', 'loop'), 'pattern' is not permitted.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}