Skip to content

Commit aa10469

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add custom session id functionality to vertex ai session service
PiperOrigin-RevId: 891919371
1 parent 78525d2 commit aa10469

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

tests/unit/vertexai/genai/replays/test_create_agent_engine_session.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ def test_create_session_with_expire_time(client):
8585
client.agent_engines.delete(name=agent_engine.api_resource.name, force=True)
8686

8787

88+
def test_create_session_with_custom_session_id(client):
89+
agent_engine = client.agent_engines.create()
90+
try:
91+
assert isinstance(agent_engine, types.AgentEngine)
92+
assert isinstance(agent_engine.api_resource, types.ReasoningEngine)
93+
94+
operation = client.agent_engines.sessions.create(
95+
name=agent_engine.api_resource.name,
96+
user_id="test-user-123",
97+
config=types.CreateAgentEngineSessionConfig(
98+
display_name="my_session",
99+
session_state={"foo": "bar"},
100+
session_id="my-session-id",
101+
),
102+
)
103+
assert isinstance(operation, types.AgentEngineSessionOperation)
104+
assert operation.response.display_name == "my_session"
105+
assert operation.response.session_state == {"foo": "bar"}
106+
assert operation.response.user_id == "test-user-123"
107+
assert (
108+
operation.response.name
109+
== f"{agent_engine.api_resource.name}/sessions/my-session-id"
110+
)
111+
assert operation.done
112+
finally:
113+
# Clean up resources.
114+
client.agent_engines.delete(name=agent_engine.api_resource.name, force=True)
115+
116+
88117
pytestmark = pytest_helper.setup(
89118
file=__file__,
90119
globals_for_file=globals(),

vertexai/_genai/sessions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def _CreateAgentEngineSessionConfig_to_vertex(
6464
if getv(from_object, ["labels"]) is not None:
6565
setv(parent_object, ["labels"], getv(from_object, ["labels"]))
6666

67+
if getv(from_object, ["session_id"]) is not None:
68+
setv(parent_object, ["_query", "sessionId"], getv(from_object, ["session_id"]))
69+
6770
return to_object
6871

6972

@@ -176,6 +179,9 @@ def _UpdateAgentEngineSessionConfig_to_vertex(
176179
if getv(from_object, ["labels"]) is not None:
177180
setv(parent_object, ["labels"], getv(from_object, ["labels"]))
178181

182+
if getv(from_object, ["session_id"]) is not None:
183+
setv(parent_object, ["_query", "sessionId"], getv(from_object, ["session_id"]))
184+
179185
if getv(from_object, ["update_mask"]) is not None:
180186
setv(
181187
parent_object, ["_query", "updateMask"], getv(from_object, ["update_mask"])

vertexai/_genai/types/common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10790,6 +10790,10 @@ class CreateAgentEngineSessionConfig(_common.BaseModel):
1079010790
default=None,
1079110791
description="""Optional. The labels with user-defined metadata to organize your Sessions. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels.""",
1079210792
)
10793+
session_id: Optional[str] = Field(
10794+
default=None,
10795+
description="""Optional. The user defined ID to use for session, which will become the final component of the session resource name. If not provided, Vertex AI will generate a value for this ID. This value may be up to 63 characters, and valid characters are `[a-z0-9-]`. The first character must be a letter, and the last character must be a letter or number.""",
10796+
)
1079310797

1079410798

1079510799
class CreateAgentEngineSessionConfigDict(TypedDict, total=False):
@@ -10818,6 +10822,9 @@ class CreateAgentEngineSessionConfigDict(TypedDict, total=False):
1081810822
labels: Optional[dict[str, str]]
1081910823
"""Optional. The labels with user-defined metadata to organize your Sessions. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels."""
1082010824

10825+
session_id: Optional[str]
10826+
"""Optional. The user defined ID to use for session, which will become the final component of the session resource name. If not provided, Vertex AI will generate a value for this ID. This value may be up to 63 characters, and valid characters are `[a-z0-9-]`. The first character must be a letter, and the last character must be a letter or number."""
10827+
1082110828

1082210829
CreateAgentEngineSessionConfigOrDict = Union[
1082310830
CreateAgentEngineSessionConfig, CreateAgentEngineSessionConfigDict
@@ -11267,6 +11274,10 @@ class UpdateAgentEngineSessionConfig(_common.BaseModel):
1126711274
default=None,
1126811275
description="""Optional. The labels with user-defined metadata to organize your Sessions. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels.""",
1126911276
)
11277+
session_id: Optional[str] = Field(
11278+
default=None,
11279+
description="""Optional. The user defined ID to use for session, which will become the final component of the session resource name. If not provided, Vertex AI will generate a value for this ID. This value may be up to 63 characters, and valid characters are `[a-z0-9-]`. The first character must be a letter, and the last character must be a letter or number.""",
11280+
)
1127011281
update_mask: Optional[str] = Field(
1127111282
default=None,
1127211283
description="""The update mask to apply. For the `FieldMask` definition, see
@@ -11303,6 +11314,9 @@ class UpdateAgentEngineSessionConfigDict(TypedDict, total=False):
1130311314
labels: Optional[dict[str, str]]
1130411315
"""Optional. The labels with user-defined metadata to organize your Sessions. Label keys and values can be no longer than 64 characters (Unicode codepoints), can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. See https://goo.gl/xmQnxf for more information and examples of labels."""
1130511316

11317+
session_id: Optional[str]
11318+
"""Optional. The user defined ID to use for session, which will become the final component of the session resource name. If not provided, Vertex AI will generate a value for this ID. This value may be up to 63 characters, and valid characters are `[a-z0-9-]`. The first character must be a letter, and the last character must be a letter or number."""
11319+
1130611320
update_mask: Optional[str]
1130711321
"""The update mask to apply. For the `FieldMask` definition, see
1130811322
https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask."""

0 commit comments

Comments
 (0)