import logging from datetime import date from pydantic import BaseModel from openai import OpenAI logger = logging.getLogger(__name__) client = OpenAI() class Transaction(BaseModel): sender: str amount: float transaction_id: str transaction_date: date document = """ Payment received from Chinedu. Amount: ₦45,000 Reference: TXN-82K91 Date: 11 August 2026 """ # gpt-4o-mini kept merging the amount and reference into one field on # messages with unusual formatting, so this stays on the full model # despite the cost. Worth revisiting once mini catches up. completion = client.beta.chat.completions.parse( model="gpt-4o", messages=[ {"role": "system", "content": "Extract the transaction details."}, {"role": "user", "content": document}, ], response_format=Transaction, ) txn = completion.choices[0].message.parsed logger.info("parsed txn %s", txn.transaction_id)