Skip to content

Commit 44cd116

Browse files
committed
feat(agents): restore 1.x agent config wiring for backward compatibility
Change-Id: I1c8b61cf21d5fef8a622b93f55411e1628845097
1 parent da1d8f1 commit 44cd116

17 files changed

Lines changed: 495 additions & 669 deletions

src/google/adk/agents/agent_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pydantic import Discriminator
2222
from pydantic import RootModel
2323
from pydantic import Tag
24+
from typing_extensions import deprecated
2425

2526
from ..features import experimental
2627
from ..features import FeatureName
@@ -68,6 +69,7 @@ def agent_config_discriminator(v: Any) -> str:
6869

6970
# Use a RootModel to represent the agent directly at the top level.
7071
# The `discriminator` is applied to the union within the RootModel.
72+
@deprecated("AgentConfig is deprecated and will be removed in future versions.")
7173
@experimental(FeatureName.AGENT_CONFIG)
7274
class AgentConfig(RootModel[ConfigsUnion]):
7375
"""The config for the YAML schema to create an agent."""

src/google/adk/agents/base_agent.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pydantic import ConfigDict
3636
from pydantic import Field
3737
from pydantic import field_validator
38+
from typing_extensions import deprecated
3839
from typing_extensions import override
3940
from typing_extensions import TypeAlias
4041

@@ -45,6 +46,7 @@
4546
from ..telemetry import _instrumentation
4647
from ..utils.context_utils import Aclosing
4748
from ..workflow._base_node import BaseNode
49+
from .base_agent_config import BaseAgentConfig
4850
from .callback_context import CallbackContext
4951
from .context import Context
5052

@@ -92,6 +94,25 @@ class BaseAgent(BaseNode):
9294
)
9395
"""The pydantic model config."""
9496

97+
config_type: ClassVar[type[BaseAgentConfig]] = BaseAgentConfig
98+
"""The config type for this agent.
99+
100+
DEPRECATED: This attribute is deprecated and will be removed in a future
101+
version, along with the AgentConfig YAML loader.
102+
103+
Sub-classes should override this to specify their own config type.
104+
105+
Example:
106+
107+
```
108+
class MyAgentConfig(BaseAgentConfig):
109+
my_field: str = ''
110+
111+
class MyAgent(BaseAgent):
112+
config_type: ClassVar[type[BaseAgentConfig]] = MyAgentConfig
113+
```
114+
"""
115+
95116
name: str
96117
"""The agent's name.
97118
@@ -619,3 +640,93 @@ def __set_parent_agent_for_sub_agents(self) -> BaseAgent:
619640
)
620641
sub_agent.parent_agent = self
621642
return self
643+
644+
@classmethod
645+
@deprecated(
646+
'BaseAgent.from_config is deprecated and will be removed in future'
647+
' versions.'
648+
)
649+
@experimental(FeatureName.AGENT_CONFIG)
650+
def from_config(
651+
cls: Type[SelfAgent],
652+
config: BaseAgentConfig,
653+
config_abs_path: str,
654+
) -> SelfAgent:
655+
"""Creates an agent from a config.
656+
657+
If sub-classes use a custom agent config, override `_parse_config` to
658+
return updated kwargs for the agent constructor.
659+
660+
Args:
661+
config: The config to create the agent from.
662+
config_abs_path: The absolute path to the config file that contains the
663+
agent config.
664+
665+
Returns:
666+
The created agent.
667+
"""
668+
kwargs = cls.__create_kwargs(config, config_abs_path)
669+
kwargs = cls._parse_config(config, config_abs_path, kwargs)
670+
return cls(**kwargs)
671+
672+
@classmethod
673+
@experimental(FeatureName.AGENT_CONFIG)
674+
def _parse_config(
675+
cls: Type[SelfAgent],
676+
config: BaseAgentConfig,
677+
config_abs_path: str,
678+
kwargs: Dict[str, Any],
679+
) -> Dict[str, Any]:
680+
"""Parses the config and returns updated kwargs to construct the agent.
681+
682+
Sub-classes should override this method to use a custom agent config class.
683+
684+
Args:
685+
config: The config to parse.
686+
config_abs_path: The absolute path to the config file that contains the
687+
agent config.
688+
kwargs: The keyword arguments used for agent constructor.
689+
690+
Returns:
691+
The updated keyword arguments used for agent constructor.
692+
"""
693+
return kwargs
694+
695+
@classmethod
696+
def __create_kwargs(
697+
cls,
698+
config: BaseAgentConfig,
699+
config_abs_path: str,
700+
) -> Dict[str, Any]:
701+
"""Creates kwargs for the fields of BaseAgent."""
702+
703+
from .config_agent_utils import resolve_agent_reference
704+
from .config_agent_utils import resolve_callbacks
705+
706+
kwargs: Dict[str, Any] = {
707+
'name': config.name,
708+
'description': config.description,
709+
}
710+
if config.sub_agents:
711+
sub_agents = []
712+
for sub_agent_config in config.sub_agents:
713+
sub_agent = resolve_agent_reference(sub_agent_config, config_abs_path)
714+
sub_agents.append(sub_agent)
715+
kwargs['sub_agents'] = sub_agents
716+
717+
if config.before_agent_callbacks:
718+
kwargs['before_agent_callback'] = resolve_callbacks(
719+
config.before_agent_callbacks
720+
)
721+
if config.after_agent_callbacks:
722+
kwargs['after_agent_callback'] = resolve_callbacks(
723+
config.after_agent_callbacks
724+
)
725+
726+
# Preserves 1.x AgentConfigMapper behavior: extra YAML fields that match
727+
# a constructor parameter pass through automatically.
728+
if config.model_extra:
729+
for key, value in config.model_extra.items():
730+
if key in cls.model_fields and key not in kwargs:
731+
kwargs[key] = value
732+
return kwargs

src/google/adk/agents/base_agent_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pydantic import BaseModel
2626
from pydantic import ConfigDict
2727
from pydantic import Field
28+
from typing_extensions import deprecated
2829

2930
from ..features import experimental
3031
from ..features import FeatureName
@@ -34,6 +35,9 @@
3435
TBaseAgentConfig = TypeVar('TBaseAgentConfig', bound='BaseAgentConfig')
3536

3637

38+
@deprecated(
39+
'BaseAgentConfig is deprecated and will be removed in future versions.'
40+
)
3741
@experimental(FeatureName.AGENT_CONFIG)
3842
class BaseAgentConfig(BaseModel):
3943
"""The config for the YAML schema of a BaseAgent.

src/google/adk/agents/common_configs.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
from typing import Any
20-
from typing import List
2119
from typing import Optional
2220

2321
from pydantic import BaseModel
@@ -28,33 +26,18 @@
2826
from ..features import FeatureName
2927

3028

31-
@experimental(FeatureName.AGENT_CONFIG)
32-
class ArgumentConfig(BaseModel):
33-
"""An argument passed to a function or a class's constructor."""
34-
35-
model_config = ConfigDict(extra="forbid")
36-
37-
name: Optional[str] = None
38-
"""Optional. The argument name.
39-
40-
When the argument is for a positional argument, this can be omitted.
41-
"""
42-
43-
value: Any
44-
"""The argument value."""
45-
46-
4729
@experimental(FeatureName.AGENT_CONFIG)
4830
class CodeConfig(BaseModel):
4931
"""Code reference config for a variable, a function, or a class.
5032
51-
This config is used for configuring callbacks and tools.
33+
Only references an object by name. YAML cannot pass constructor args; to
34+
use a configured object, build it in Python and reference its FQN here.
5235
"""
5336

5437
model_config = ConfigDict(extra="forbid")
5538

5639
name: str
57-
"""Required. The name of the variable, function, class, etc. in code.
40+
"""Required. The fully qualified name of the variable, function, or class.
5841
5942
Examples:
6043
@@ -65,22 +48,6 @@ class CodeConfig(BaseModel):
6548
When used for callbacks, it refers to a function, e.g. `my_library.my_callbacks.my_callback`
6649
"""
6750

68-
args: Optional[List[ArgumentConfig]] = None
69-
"""Optional. The arguments for the code when `name` refers to a function or a
70-
class's constructor.
71-
72-
Examples:
73-
```
74-
tools
75-
- name: AgentTool
76-
args:
77-
- name: agent
78-
value: search_agent.yaml
79-
- name: skip_summarization
80-
value: True
81-
```
82-
"""
83-
8451

8552
@experimental(FeatureName.AGENT_CONFIG)
8653
class AgentRefConfig(BaseModel):

0 commit comments

Comments
 (0)