Skip to content

Building AI agents, atomically

resolve schema properties returning BaseIOSchema instead of custom types

KennyVaneetvelde86e83968a672
diff --git a/atomic-agents/atomic_agents/agents/atomic_agent.py b/atomic-agents/atomic_agents/agents/atomic_agent.py
index 0f4d5e6..34f5528 100644
--- a/atomic-agents/atomic_agents/agents/atomic_agent.py
+++ b/atomic-agents/atomic_agents/agents/atomic_agent.py
@@ -94,6 +94,23 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
- Use this for parameters like 'temperature', 'max_tokens', etc.
"""
+ def __init_subclass__(cls, **kwargs):
+ """
+ Hook called when a class is subclassed.
+
+ Captures generic type parameters during class creation and stores them as class attributes
+ to work around the unreliable __orig_class__ attribute in modern Python generic syntax.
+ """
+ super().__init_subclass__(**kwargs)
+ if hasattr(cls, "__orig_bases__"):
+ for base in cls.__orig_bases__:
+ if hasattr(base, "__origin__") and base.__origin__ is AtomicAgent:
+ args = get_args(base)
+ if len(args) == 2:
+ cls._input_schema_cls = args[0]
+ cls._output_schema_cls = args[1]
+ break
+
def __init__(self, config: AgentConfig):
"""
Initializes the AtomicAgent.
@@ -118,21 +135,11 @@ class AtomicAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
@property
def input_schema(self) -> Type[BaseIOSchema]:
- if hasattr(self, "__orig_class__"):
- TI, _ = get_args(self.__orig_class__)
- else:
- TI = BasicChatInputSchema
-
- return TI
+ return getattr(self.__class__, "_input_schema_cls", BasicChatInputSchema)
@property
def output_schema(self) -> Type[BaseIOSchema]:
- if hasattr(self, "__orig_class__"):
- _, TO = get_args(self.__orig_class__)
- else:
- TO = BasicChatOutputSchema
-
- return TO
+ return getattr(self.__class__, "_output_schema_cls", BasicChatOutputSchema)
def _prepare_messages(self):
if self.system_role is None:
diff --git a/atomic-agents/atomic_agents/base/base_tool.py b/atomic-agents/atomic_agents/base/base_tool.py
index 9808c4b..967e051 100644
--- a/atomic-agents/atomic_agents/base/base_tool.py
+++ b/atomic-agents/atomic_agents/base/base_tool.py
@@ -38,6 +38,23 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
tool_description (str): Description of the tool, derived from the input schema's description or overridden by the config.
"""
+ def __init_subclass__(cls, **kwargs):
+ """
+ Hook called when a class is subclassed.
+
+ Captures generic type parameters during class creation and stores them as class attributes
+ to work around the unreliable __orig_class__ attribute in modern Python generic syntax.
+ """
+ super().__init_subclass__(**kwargs)
+ if hasattr(cls, "__orig_bases__"):
+ for base in cls.__orig_bases__:
+ if hasattr(base, "__origin__") and base.__origin__ is BaseTool:
+ args = get_args(base)
+ if len(args) == 2:
+ cls._input_schema_cls = args[0]
+ cls._output_schema_cls = args[1]
+ break
+
def __init__(self, config: BaseToolConfig = BaseToolConfig()):
"""
Initializes the BaseTool with an optional configuration override.
@@ -55,12 +72,7 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
Returns:
Type[InputSchema]: The input schema class.
"""
- if hasattr(self, "__orig_class__"):
- TI, _ = get_args(self.__orig_class__)
- else:
- TI = BaseIOSchema
-
- return TI
+ return getattr(self.__class__, "_input_schema_cls", BaseIOSchema)
@property
def output_schema(self) -> Type[OutputSchema]:
@@ -70,12 +82,7 @@ class BaseTool[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema](ABC):
Returns:
Type[OutputSchema]: The output schema class.
"""
- if hasattr(self, "__orig_class__"):
- _, TO = get_args(self.__orig_class__)
- else:
- TO = BaseIOSchema
-
- return TO
+ return getattr(self.__class__, "_output_schema_cls", BaseIOSchema)
@property
def tool_name(self) -> str: