fix: reject negative recent-event limits
Merge https://github.com/google/adk-python/pull/6687 Unsupported negative limits should fail at configuration time, before a session backend handles them. PiperOrigin-RevId: 968610704
This commit is contained in:
committed by
Copybara-Service
parent
d18df2fa1c
commit
26110c7559
@@ -20,6 +20,7 @@ from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
from pydantic import Field
|
||||
from pydantic import field_validator
|
||||
|
||||
from ..events.event import Event
|
||||
from .session import Session
|
||||
@@ -32,7 +33,8 @@ class GetSessionConfig(BaseModel):
|
||||
Attributes:
|
||||
num_recent_events: The limit of recent events to get for the session.
|
||||
Optional: if None, the filter is not applied; if greater than 0, returns
|
||||
at most given number of recent events; if 0, no events are returned.
|
||||
at most given number of recent events; if 0, no events are returned;
|
||||
if negative, a ValueError is raised.
|
||||
after_timestamp: The earliest timestamp of events to get for the session.
|
||||
Optional: if None, the filter is not applied; otherwise, returns events
|
||||
with timestamp >= the given time.
|
||||
@@ -41,6 +43,13 @@ class GetSessionConfig(BaseModel):
|
||||
num_recent_events: Optional[int] = None
|
||||
after_timestamp: Optional[float] = None
|
||||
|
||||
@field_validator('num_recent_events')
|
||||
@classmethod
|
||||
def _validate_num_recent_events(cls, value: Optional[int]) -> Optional[int]:
|
||||
if value is not None and value < 0:
|
||||
raise ValueError('num_recent_events must be greater than or equal to 0.')
|
||||
return value
|
||||
|
||||
|
||||
class ListSessionsResponse(BaseModel):
|
||||
"""The response of listing sessions.
|
||||
|
||||
@@ -55,6 +55,12 @@ from . import _conformance
|
||||
from ._conformance import session_service # noqa: F401
|
||||
|
||||
|
||||
def test_get_session_config_rejects_negative_num_recent_events():
|
||||
"""A negative recent-event limit is rejected at configuration time."""
|
||||
with pytest.raises(ValueError, match='greater than or equal to 0'):
|
||||
GetSessionConfig(num_recent_events=-1)
|
||||
|
||||
|
||||
class SessionServiceType(enum.Enum):
|
||||
IN_MEMORY = 'IN_MEMORY'
|
||||
DATABASE = 'DATABASE'
|
||||
|
||||
Reference in New Issue
Block a user