|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from decimal import Decimal |
3 | 4 | from enum import Enum |
4 | 5 |
|
5 | 6 | from pydantic import Field, BaseModel |
@@ -409,3 +410,73 @@ def test_nested_inline_ref_expansion() -> None: |
409 | 410 | "additionalProperties": False, |
410 | 411 | } |
411 | 412 | ) |
| 413 | + |
| 414 | + |
| 415 | +class InsuranceQuote(BaseModel): |
| 416 | + """Test model with Decimal field to verify pattern keyword is stripped""" |
| 417 | + premium: Decimal = Field(description="The insurance premium amount") |
| 418 | + coverage_amount: float = Field(description="The coverage amount") |
| 419 | + customer_name: str = Field(description="The customer's name") |
| 420 | + |
| 421 | + |
| 422 | +def test_decimal_field_strips_pattern() -> None: |
| 423 | + """ |
| 424 | + Test that Decimal fields do not include unsupported 'pattern' keyword. |
| 425 | +
|
| 426 | + Pydantic generates a regex pattern for Decimal fields by default, but this |
| 427 | + is not supported by OpenAI's structured outputs in strict mode. This test |
| 428 | + verifies that the pattern keyword is properly stripped from the schema. |
| 429 | +
|
| 430 | + Fixes issue #2718 |
| 431 | + """ |
| 432 | + if not PYDANTIC_V1: |
| 433 | + schema = to_strict_json_schema(InsuranceQuote) |
| 434 | + |
| 435 | + # Verify the schema structure exists |
| 436 | + assert "properties" in schema |
| 437 | + assert "premium" in schema["properties"] |
| 438 | + |
| 439 | + # Get the premium field schema |
| 440 | + premium_schema = schema["properties"]["premium"] |
| 441 | + |
| 442 | + # Verify it's an anyOf with number/string/null options |
| 443 | + assert "anyOf" in premium_schema |
| 444 | + |
| 445 | + # Check all variants in the anyOf for 'pattern' keyword |
| 446 | + # Pattern should NOT be present after our fix |
| 447 | + for variant in premium_schema["anyOf"]: |
| 448 | + assert "pattern" not in variant, ( |
| 449 | + "Pattern keyword should be stripped from Decimal field schema. " |
| 450 | + "Found pattern in variant: " + str(variant) |
| 451 | + ) |
| 452 | + |
| 453 | + # Verify the schema matches expected structure (without pattern) |
| 454 | + assert schema == snapshot( |
| 455 | + { |
| 456 | + "title": "InsuranceQuote", |
| 457 | + "type": "object", |
| 458 | + "properties": { |
| 459 | + "premium": { |
| 460 | + "anyOf": [ |
| 461 | + {"type": "number"}, |
| 462 | + {"type": "string"}, |
| 463 | + {"type": "null"} |
| 464 | + ], |
| 465 | + "description": "The insurance premium amount", |
| 466 | + "title": "Premium", |
| 467 | + }, |
| 468 | + "coverage_amount": { |
| 469 | + "description": "The coverage amount", |
| 470 | + "title": "Coverage Amount", |
| 471 | + "type": "number", |
| 472 | + }, |
| 473 | + "customer_name": { |
| 474 | + "description": "The customer's name", |
| 475 | + "title": "Customer Name", |
| 476 | + "type": "string", |
| 477 | + }, |
| 478 | + }, |
| 479 | + "required": ["premium", "coverage_amount", "customer_name"], |
| 480 | + "additionalProperties": False, |
| 481 | + } |
| 482 | + ) |
0 commit comments