Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 3 additions & 6 deletions src/google/adk/optimization/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
class SamplingResult(BaseModel):
"""Base class for evaluation results of the candidate agent on the batch of examples."""

scores: dict[str, float] = Field( # type: ignore[call-overload]
required=True,
scores: dict[str, float] = Field(
description=(
"A map from example UID to the agent's overall score on that example."
" (higher is better)."
Expand All @@ -49,8 +48,7 @@ class AgentWithScores(BaseModel):
sub-classing this class.
"""

optimized_agent: Agent = Field( # type: ignore[call-overload]
required=True,
optimized_agent: Agent = Field(
description="The optimized agent.",
)

Expand All @@ -66,8 +64,7 @@ class AgentWithScores(BaseModel):
class OptimizerResult(BaseModel, Generic[AgentWithScoresT]):
"""Base class for optimizer final results."""

optimized_agents: list[AgentWithScoresT] = Field( # type: ignore[call-overload]
required=True,
optimized_agents: list[AgentWithScoresT] = Field(
description=(
"A list of optimized agents which cannot be considered strictly"
" better than one another (see"
Expand Down
9 changes: 3 additions & 6 deletions src/google/adk/optimization/local_eval_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,15 @@ def extract_single_invocation_info(
class LocalEvalSamplerConfig(BaseModel):
"""Contains configuration options required by the LocalEvalServiceInterface."""

eval_config: EvalConfig = Field( # type: ignore[call-overload]
required=True,
eval_config: EvalConfig = Field(
description="The configuration for the evaluation.",
)

app_name: str = Field( # type: ignore[call-overload]
required=True,
app_name: str = Field(
description="The app name to use for evaluation.",
)

train_eval_set: str = Field( # type: ignore[call-overload]
required=True,
train_eval_set: str = Field(
description="The name of the eval set to use for optimization.",
)

Expand Down
67 changes: 67 additions & 0 deletions tests/unittests/optimization/data_types_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from google.adk.optimization.data_types import AgentWithScores
from google.adk.optimization.data_types import OptimizerResult
from google.adk.optimization.data_types import SamplingResult
from google.adk.optimization.local_eval_sampler import LocalEvalSamplerConfig
from pydantic import BaseModel
from pydantic import ValidationError
import pytest


@pytest.mark.parametrize(
("model", "field_names"),
[
(SamplingResult, ["scores"]),
(LocalEvalSamplerConfig, ["eval_config", "app_name", "train_eval_set"]),
],
)
def test_json_schema_marks_required_fields_at_object_level(
model: type[BaseModel], field_names: list[str]
):
schema = model.model_json_schema()

for field_name in field_names:
# JSON Schema's `required` is an array on the object, so a boolean
# `required` inside a property schema is invalid.
assert "required" not in schema["properties"][field_name]
assert field_name in schema["required"]


@pytest.mark.parametrize(
("model", "field_name"),
[
(SamplingResult, "scores"),
(AgentWithScores, "optimized_agent"),
(OptimizerResult, "optimized_agents"),
(LocalEvalSamplerConfig, "eval_config"),
(LocalEvalSamplerConfig, "app_name"),
(LocalEvalSamplerConfig, "train_eval_set"),
],
)
def test_fields_without_default_are_required(
model: type[BaseModel], field_name: str
):
with pytest.raises(ValidationError) as exc_info:
model.model_validate({})

missing = {
error["loc"][0]
for error in exc_info.value.errors()
if error["type"] == "missing"
}
assert field_name in missing
Loading