Skip to content

Commit bea67c2

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add session_id to Create Session to allow custom session id
PiperOrigin-RevId: 893060846
1 parent c12aedc commit bea67c2

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
@@ -10810,6 +10810,10 @@ class CreateAgentEngineSessionConfig(_common.BaseModel):
1081010810
default=None,
1081110811
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.""",
1081210812
)
10813+
session_id: Optional[str] = Field(
10814+
default=None,
10815+
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.""",
10816+
)
1081310817

1081410818

1081510819
class CreateAgentEngineSessionConfigDict(TypedDict, total=False):
@@ -10838,6 +10842,9 @@ class CreateAgentEngineSessionConfigDict(TypedDict, total=False):
1083810842
labels: Optional[dict[str, str]]
1083910843
"""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."""
1084010844

10845+
session_id: Optional[str]
10846+
"""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."""
10847+
1084110848

1084210849
CreateAgentEngineSessionConfigOrDict = Union[
1084310850
CreateAgentEngineSessionConfig, CreateAgentEngineSessionConfigDict
@@ -11287,6 +11294,10 @@ class UpdateAgentEngineSessionConfig(_common.BaseModel):
1128711294
default=None,
1128811295
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.""",
1128911296
)
11297+
session_id: Optional[str] = Field(
11298+
default=None,
11299+
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.""",
11300+
)
1129011301
update_mask: Optional[str] = Field(
1129111302
default=None,
1129211303
description="""The update mask to apply. For the `FieldMask` definition, see
@@ -11323,6 +11334,9 @@ class UpdateAgentEngineSessionConfigDict(TypedDict, total=False):
1132311334
labels: Optional[dict[str, str]]
1132411335
"""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."""
1132511336

11337+
session_id: Optional[str]
11338+
"""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."""
11339+
1132611340
update_mask: Optional[str]
1132711341
"""The update mask to apply. For the `FieldMask` definition, see
1132811342
https://protobuf.dev/reference/protobuf/google.protobuf/#field-mask."""

0 commit comments

Comments
 (0)