11import typing
2+ import uuid
23
34from . import EmbedByTypeResponseEmbeddings
4- from .core .pydantic_utilities import _get_model_fields , Model
5+ from .core .pydantic_utilities import _get_model_fields , Model , IS_PYDANTIC_V2
56
67from pprint import pprint
78
@@ -33,6 +34,31 @@ def allow_access_to_aliases(self: typing.Type["Model"], name):
3334 f"'{ type (self ).__name__ } ' object has no attribute '{ name } '" )
3435
3536
37+ def make_tool_call_v2_id_optional (cls ):
38+ """
39+ Override ToolCallV2 to make the 'id' field optional with a default UUID.
40+ This ensures backward compatibility with code that doesn't provide an id.
41+
42+ We wrap the __init__ method to inject a default id before Pydantic validation runs.
43+ """
44+ # Store the original __init__ method
45+ original_init = cls .__init__
46+
47+ def patched_init (self , / , ** data ):
48+ """Patched __init__ that injects default id if not provided."""
49+ # Inject default UUID if 'id' is not in the data
50+ if 'id' not in data :
51+ data ['id' ] = str (uuid .uuid4 ())
52+
53+ # Call the original __init__ with modified data
54+ original_init (self , ** data )
55+
56+ # Replace the __init__ method
57+ cls .__init__ = patched_init
58+
59+ return cls
60+
61+
3662def run_overrides ():
3763 """
3864 These are overrides to allow us to make changes to generated code without touching the generated files themselves.
@@ -41,3 +67,14 @@ def run_overrides():
4167
4268 # Override to allow access to aliases in EmbedByTypeResponseEmbeddings eg embeddings.float rather than embeddings.float_
4369 setattr (EmbedByTypeResponseEmbeddings , "__getattr__" , allow_access_to_aliases )
70+
71+ # Import ToolCallV2 lazily to avoid circular dependency issues
72+ from . import ToolCallV2
73+
74+ # Override ToolCallV2 to make id field optional with default UUID
75+ make_tool_call_v2_id_optional (ToolCallV2 )
76+
77+
78+ # Run overrides immediately at module import time to ensure they're applied
79+ # before any code tries to use the modified classes
80+ run_overrides ()
0 commit comments