Skip to content

Commit 91d2733

Browse files
committed
Update manually maintained files to ensure backward compatibility
1 parent 9492840 commit 91d2733

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

.fernignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ src/cohere/utils.py
1111
src/cohere/overrides.py
1212
src/cohere/config.py
1313
src/cohere/manually_maintained
14+
src/cohere/manually_maintained/__init__.py
1415
src/cohere/bedrock_client.py
1516
src/cohere/aws_client.py
1617
src/cohere/sagemaker_client.py

src/cohere/aliases.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Import overrides early to ensure they're applied before types are used
2+
# This is necessary for backwards compatibility patches like ToolCallV2.id being optional
3+
from . import overrides # noqa: F401
4+
15
from .v2 import (
26
ContentDeltaV2ChatStreamResponse,
37
ContentEndV2ChatStreamResponse,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This module ensures overrides are applied early in the import process
2+
# Import overrides to trigger backwards compatibility patches
3+
from .. import overrides # noqa: F401

src/cohere/overrides.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import typing
2+
import uuid
23

34
from . 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

67
from 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+
3662
def 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()

src/cohere/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from .datasets import DatasetsCreateResponse, DatasetsGetResponse
1414
from .overrides import get_fields
1515

16+
# Note: utils.py does NOT call run_overrides() itself - that's done in client.py
17+
# which imports utils.py. This ensures overrides are applied when client is used.
18+
1619

1720
def get_terminal_states():
1821
return get_success_states() | get_failed_states()

0 commit comments

Comments
 (0)