Skip to content

Fix: Make Flags.from_dict generic to support all subclasses #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/agentlab/agents/dynamic_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import platform
import time
from copy import copy, deepcopy
from dataclasses import asdict, dataclass
from dataclasses import asdict, dataclass, fields
from textwrap import dedent
from typing import Literal
from warnings import warn
Expand Down Expand Up @@ -34,14 +34,21 @@ def asdict(self):
return asdict(self)

@classmethod
def from_dict(self, flags_dict):
def from_dict(cls, flags_dict):
"""Helper for JSON serializable requirement."""
if isinstance(flags_dict, ObsFlags):
if isinstance(flags_dict, cls):
return flags_dict

if not isinstance(flags_dict, dict):
raise ValueError(f"Unregcognized type for flags_dict of type {type(flags_dict)}.")
return ObsFlags(**flags_dict)
raise ValueError(f"Unrecognized type for flags_dict of type {type(flags_dict)}.")

# Get the names of the fields of the dataclass
class_fields = {f.name for f in fields(cls)}

# Filter the dictionary to only include keys that are fields of the class
filtered_dict = {k: v for k, v in flags_dict.items() if k in class_fields}

return cls(**filtered_dict)


@dataclass
Expand Down