Skip to content

Commit

Permalink
fix: make sure server event does not break when it has extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasliu-agora committed Dec 9, 2024
1 parent cbc54e2 commit 20efb30
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion realtime_agent/realtime/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,9 @@ def from_dict(data_class, data):
"""Recursively convert a dictionary to a dataclass instance."""
if is_dataclass(data_class): # Check if the target class is a dataclass
fieldtypes = {f.name: f.type for f in data_class.__dataclass_fields__.values()}
return data_class(**{f: from_dict(fieldtypes[f], data[f]) for f in data})
# Filter out keys that are not in the dataclass fields
valid_data = {f: data[f] for f in fieldtypes if f in data}
return data_class(**{f: from_dict(fieldtypes[f], valid_data[f]) for f in valid_data})
elif isinstance(data, list): # Handle lists of nested dataclass objects
return [from_dict(data_class.__args__[0], item) for item in data]
else: # For primitive types (str, int, float, etc.), return the value as-is
Expand Down

0 comments on commit 20efb30

Please sign in to comment.