(no commit message)

This commit is contained in:
2025-11-04 11:46:40 -05:00
parent 0074689e91
commit ea49e4a9e7

View File

@@ -1,6 +1,6 @@
import dspy import dspy
from enum import Enum from enum import Enum
from typing import Any, Dict, List, Optional, Literal from typing import Any, Dict, List, Optional, Literal, Union, get_origin
from pydantic import BaseModel, Field, ValidationError from pydantic import BaseModel, Field, ValidationError
class FieldType(str, Enum): class FieldType(str, Enum):
@@ -80,8 +80,8 @@ class PydanticModelSchema(BaseModel):
else: else:
type_annotation = field_def.type.value type_annotation = field_def.type.value
# Add Optional wrapper if not required # Add Optional wrapper if not required (but avoid double-wrapping)
if not field_def.required: if not field_def.required and not type_annotation.startswith("Optional["):
type_annotation = f"Optional[{type_annotation}]" type_annotation = f"Optional[{type_annotation}]"
# Build Field() arguments # Build Field() arguments
@@ -331,8 +331,9 @@ class SignatureGenerator(dspy.Module):
} }
py_type = type_map.get(type_str, str) py_type = type_map.get(type_str, str)
# Wrap in Optional if not required # Wrap in Optional if not required (but avoid double-wrapping)
if not field_def.required and not isinstance(py_type, type(Optional[str])): # Optional[X] is Union[X, None], so check if already a Union type
if not field_def.required and get_origin(py_type) is not Union:
py_type = Optional[py_type] py_type = Optional[py_type]
# Create Pydantic field # Create Pydantic field