Skip to content

Commit f85b9f9

Browse files
speedstorm1copybara-github
authored andcommitted
feat: allow users to configure max wait time for prompt management queries for Python and JS
FUTURE_COPYBARA_INTEGRATE_REVIEW=#7003 from googleapis:release-please--branches--main 5905f2d PiperOrigin-RevId: 944229591
1 parent 922fa6a commit f85b9f9

2 files changed

Lines changed: 112 additions & 18 deletions

File tree

agentplatform/_genai/prompts.py

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,10 @@ def create(
15641564
)
15651565
dataset_resource_name = self._wait_for_operation(
15661566
operation=create_prompt_dataset_operation,
1567-
timeout=config.timeout if config else 90,
1567+
timeout=config.timeout if config and config.timeout else 90,
1568+
max_wait_time=(
1569+
config.max_wait_time if config and config.max_wait_time else 60
1570+
),
15681571
)
15691572
dataset_id = dataset_resource_name.split("/")[-1]
15701573

@@ -1636,7 +1639,10 @@ def create_version(
16361639
)
16371640
dataset_resource_name = self._wait_for_operation(
16381641
operation=create_prompt_dataset_operation,
1639-
timeout=config.timeout if config else 90,
1642+
timeout=config.timeout if config and config.timeout else 90,
1643+
max_wait_time=(
1644+
config.max_wait_time if config and config.max_wait_time else 60
1645+
),
16401646
)
16411647
dataset_id = dataset_resource_name.split("/")[-1]
16421648

@@ -1660,7 +1666,10 @@ def create_version(
16601666
)
16611667
dataset_version_resource_name = self._wait_for_operation(
16621668
operation=create_dataset_version_operation,
1663-
timeout=config.timeout if config else 90,
1669+
timeout=config.timeout if config and config.timeout else 90,
1670+
max_wait_time=(
1671+
config.max_wait_time if config and config.max_wait_time else 60
1672+
),
16641673
)
16651674

16661675
# Step 4: Get the dataset version resource and return it with the prompt
@@ -1679,12 +1688,14 @@ def _wait_for_operation(
16791688
self,
16801689
operation: types.DatasetOperation,
16811690
timeout: int,
1691+
max_wait_time: int = 60,
16821692
) -> str:
16831693
"""Waits for a dataset operation to complete.
16841694
16851695
Args:
16861696
operation: The dataset operation to wait for.
16871697
timeout: The maximum time to wait for the operation to complete.
1698+
max_wait_time: The maximum interval between polling requests in seconds.
16881699
16891700
Returns:
16901701
The name of the Dataset resource from the operation result.
@@ -1706,7 +1717,6 @@ def _wait_for_operation(
17061717
start_time = time.time()
17071718
sleep_duration = 5
17081719
wait_multiplier = 2
1709-
max_wait_time = 60
17101720
previous_time = time.time()
17111721

17121722
while not done:
@@ -1923,6 +1933,7 @@ def _wait_for_project_operation(
19231933
self,
19241934
operation: genai_types.ProjectOperation,
19251935
timeout: int,
1936+
max_wait_time: int = 60,
19261937
) -> None:
19271938
"""Waits for a dataset deletion operation to complete.
19281939
@@ -1931,6 +1942,7 @@ def _wait_for_project_operation(
19311942
Args:
19321943
operation: The project operation to wait for.
19331944
timeout: The maximum time to wait for the operation to complete.
1945+
max_wait_time: The maximum interval between polling requests in seconds.
19341946
Raises:
19351947
TimeoutError: If the operation does not complete within the timeout.
19361948
ValueError: If the operation fails.
@@ -1940,7 +1952,6 @@ def _wait_for_project_operation(
19401952
start_time = time.time()
19411953
sleep_duration = 5
19421954
wait_multiplier = 2
1943-
max_wait_time = 60
19441955
previous_time = time.time()
19451956
while not done:
19461957
if (time.time() - start_time) > timeout:
@@ -1985,7 +1996,11 @@ def delete(
19851996
config=config,
19861997
)
19871998
self._wait_for_project_operation(
1988-
operation=delete_prompt_operation, timeout=config.timeout if config else 90
1999+
operation=delete_prompt_operation,
2000+
timeout=config.timeout if config and config.timeout else 90,
2001+
max_wait_time=(
2002+
config.max_wait_time if config and config.max_wait_time else 60
2003+
),
19892004
)
19902005
logger.info(f"Deleted prompt with id: {prompt_id}")
19912006

@@ -2013,7 +2028,11 @@ def delete_version(
20132028
)
20142029

20152030
self._wait_for_project_operation(
2016-
operation=delete_version_operation, timeout=config.timeout if config else 90
2031+
operation=delete_version_operation,
2032+
timeout=config.timeout if config and config.timeout else 90,
2033+
max_wait_time=(
2034+
config.max_wait_time if config and config.max_wait_time else 60
2035+
),
20172036
)
20182037
logger.info(
20192038
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
@@ -2040,10 +2059,14 @@ def restore_version(
20402059
restore_prompt_operation = self._restore_version(
20412060
dataset_id=prompt_id,
20422061
version_id=version_id,
2062+
config=config,
20432063
)
20442064
self._wait_for_project_operation(
20452065
operation=restore_prompt_operation,
2046-
timeout=90,
2066+
timeout=config.timeout if config and config.timeout else 90,
2067+
max_wait_time=(
2068+
config.max_wait_time if config and config.max_wait_time else 60
2069+
),
20472070
)
20482071
dataset_version_resource = self._get_dataset_version_resource(
20492072
dataset_id=prompt_id,
@@ -2400,7 +2423,10 @@ def update(
24002423
)
24012424
dataset_version_resource_name = self._wait_for_operation(
24022425
operation=create_dataset_version_operation,
2403-
timeout=config.timeout if config else 90,
2426+
timeout=config.timeout if config and config.timeout else 90,
2427+
max_wait_time=(
2428+
config.max_wait_time if config and config.max_wait_time else 60
2429+
),
24042430
)
24052431
dataset_version_id = dataset_version_resource_name.split("/")[-1]
24062432

@@ -3558,7 +3584,10 @@ async def create(
35583584
)
35593585
dataset_resource_name = await self._wait_for_operation(
35603586
operation=create_prompt_dataset_operation,
3561-
timeout=config.timeout if config else 90,
3587+
timeout=config.timeout if config and config.timeout else 90,
3588+
max_wait_time=(
3589+
config.max_wait_time if config and config.max_wait_time else 60
3590+
),
35623591
)
35633592
dataset_id = dataset_resource_name.split("/")[-1]
35643593

@@ -3629,7 +3658,10 @@ async def create_version(
36293658
)
36303659
dataset_resource_name = await self._wait_for_operation(
36313660
operation=create_prompt_dataset_operation,
3632-
timeout=config.timeout if config else 90,
3661+
timeout=config.timeout if config and config.timeout else 90,
3662+
max_wait_time=(
3663+
config.max_wait_time if config and config.max_wait_time else 60
3664+
),
36333665
)
36343666
dataset_id = dataset_resource_name.split("/")[-1]
36353667

@@ -3653,7 +3685,10 @@ async def create_version(
36533685
)
36543686
dataset_version_resource_name = await self._wait_for_operation(
36553687
operation=create_dataset_version_operation,
3656-
timeout=config.timeout if config else 90,
3688+
timeout=config.timeout if config and config.timeout else 90,
3689+
max_wait_time=(
3690+
config.max_wait_time if config and config.max_wait_time else 60
3691+
),
36573692
)
36583693

36593694
# Step 4: Get the dataset version resource and return it with the prompt
@@ -3740,7 +3775,10 @@ async def update(
37403775
)
37413776
dataset_version_resource_name = await self._wait_for_operation(
37423777
operation=create_dataset_version_operation,
3743-
timeout=config.timeout if config else 90,
3778+
timeout=config.timeout if config and config.timeout else 90,
3779+
max_wait_time=(
3780+
config.max_wait_time if config and config.max_wait_time else 60
3781+
),
37443782
)
37453783
dataset_version_id = dataset_version_resource_name.split("/")[-1]
37463784

@@ -3760,12 +3798,14 @@ async def _wait_for_operation(
37603798
self,
37613799
operation: types.DatasetOperation,
37623800
timeout: int,
3801+
max_wait_time: int = 60,
37633802
) -> str:
37643803
"""Waits for a dataset operation to complete.
37653804
37663805
Args:
37673806
operation: The dataset operation to wait for.
37683807
timeout: The maximum time to wait for the operation to complete.
3808+
max_wait_time: The maximum interval between polling requests in seconds.
37693809
37703810
Returns:
37713811
The name of the Dataset resource from the operation result.
@@ -3787,7 +3827,6 @@ async def _wait_for_operation(
37873827
start_time = time.time()
37883828
sleep_duration = 5
37893829
wait_multiplier = 2
3790-
max_wait_time = 60
37913830
previous_time = time.time()
37923831

37933832
while not done:
@@ -3885,6 +3924,7 @@ async def _wait_for_project_operation(
38853924
self,
38863925
operation: genai_types.ProjectOperation,
38873926
timeout: int,
3927+
max_wait_time: int = 60,
38883928
) -> None:
38893929
"""Waits for a dataset deletion operation to complete.
38903930
@@ -3893,6 +3933,7 @@ async def _wait_for_project_operation(
38933933
Args:
38943934
operation: The project operation to wait for.
38953935
timeout: The maximum time to wait for the operation to complete.
3936+
max_wait_time: The maximum interval between polling requests in seconds.
38963937
Raises:
38973938
TimeoutError: If the operation does not complete within the timeout.
38983939
ValueError: If the operation fails.
@@ -3902,7 +3943,6 @@ async def _wait_for_project_operation(
39023943
start_time = time.time()
39033944
sleep_duration = 5
39043945
wait_multiplier = 2
3905-
max_wait_time = 60
39063946
previous_time = time.time()
39073947
while not done:
39083948
if (time.time() - start_time) > timeout:
@@ -3947,7 +3987,11 @@ async def delete(
39473987
config=config,
39483988
)
39493989
await self._wait_for_project_operation(
3950-
operation=delete_prompt_operation, timeout=config.timeout if config else 90
3990+
operation=delete_prompt_operation,
3991+
timeout=config.timeout if config and config.timeout else 90,
3992+
max_wait_time=(
3993+
config.max_wait_time if config and config.max_wait_time else 60
3994+
),
39513995
)
39523996
logger.info(f"Deleted prompt with id: {prompt_id}")
39533997

@@ -3975,7 +4019,11 @@ async def delete_version(
39754019
)
39764020

39774021
await self._wait_for_project_operation(
3978-
operation=delete_version_operation, timeout=config.timeout if config else 90
4022+
operation=delete_version_operation,
4023+
timeout=config.timeout if config and config.timeout else 90,
4024+
max_wait_time=(
4025+
config.max_wait_time if config and config.max_wait_time else 60
4026+
),
39794027
)
39804028
logger.info(
39814029
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
@@ -4111,10 +4159,14 @@ async def restore_version(
41114159
restore_prompt_operation = await self._restore_version(
41124160
dataset_id=prompt_id,
41134161
version_id=version_id,
4162+
config=config,
41144163
)
41154164
await self._wait_for_project_operation(
41164165
operation=restore_prompt_operation,
4117-
timeout=90,
4166+
timeout=config.timeout if config and config.timeout else 90,
4167+
max_wait_time=(
4168+
config.max_wait_time if config and config.max_wait_time else 60
4169+
),
41184170
)
41194171
dataset_version_resource = await self._get_dataset_version_resource(
41204172
dataset_id=prompt_id,

agentplatform/_genai/types/common.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21104,6 +21104,10 @@ class DeletePromptConfig(_common.BaseModel):
2110421104
default=90,
2110521105
description="""Timeout for the delete prompt operation in seconds. Defaults to 90.""",
2110621106
)
21107+
max_wait_time: Optional[int] = Field(
21108+
default=60,
21109+
description="""Maximum interval between polling requests in seconds. Defaults to 60.""",
21110+
)
2110721111

2110821112

2110921113
class DeletePromptConfigDict(TypedDict, total=False):
@@ -21115,6 +21119,9 @@ class DeletePromptConfigDict(TypedDict, total=False):
2111521119
timeout: Optional[int]
2111621120
"""Timeout for the delete prompt operation in seconds. Defaults to 90."""
2111721121

21122+
max_wait_time: Optional[int]
21123+
"""Maximum interval between polling requests in seconds. Defaults to 60."""
21124+
2111821125

2111921126
DeletePromptConfigOrDict = Union[DeletePromptConfig, DeletePromptConfigDict]
2112021127

@@ -21262,6 +21269,14 @@ class RestoreVersionConfig(_common.BaseModel):
2126221269
http_options: Optional[genai_types.HttpOptions] = Field(
2126321270
default=None, description="""Used to override HTTP request options."""
2126421271
)
21272+
timeout: Optional[int] = Field(
21273+
default=90,
21274+
description="""Timeout for the restore prompt version operation in seconds. Defaults to 90.""",
21275+
)
21276+
max_wait_time: Optional[int] = Field(
21277+
default=60,
21278+
description="""Maximum interval between polling requests in seconds. Defaults to 60.""",
21279+
)
2126521280

2126621281

2126721282
class RestoreVersionConfigDict(TypedDict, total=False):
@@ -21270,6 +21285,12 @@ class RestoreVersionConfigDict(TypedDict, total=False):
2127021285
http_options: Optional[genai_types.HttpOptions]
2127121286
"""Used to override HTTP request options."""
2127221287

21288+
timeout: Optional[int]
21289+
"""Timeout for the restore prompt version operation in seconds. Defaults to 90."""
21290+
21291+
max_wait_time: Optional[int]
21292+
"""Maximum interval between polling requests in seconds. Defaults to 60."""
21293+
2127321294

2127421295
RestoreVersionConfigOrDict = Union[RestoreVersionConfig, RestoreVersionConfigDict]
2127521296

@@ -21367,6 +21388,10 @@ class UpdatePromptConfig(_common.BaseModel):
2136721388
default=None,
2136821389
description="""Customer-managed encryption key spec for a prompt dataset. If set, this prompt dataset and all sub-resources of this prompt dataset will be secured by this key.""",
2136921390
)
21391+
max_wait_time: Optional[int] = Field(
21392+
default=60,
21393+
description="""The maximum interval between polling requests in seconds. If not set, the default interval is 60 seconds.""",
21394+
)
2137021395

2137121396

2137221397
class UpdatePromptConfigDict(TypedDict, total=False):
@@ -21387,6 +21412,9 @@ class UpdatePromptConfigDict(TypedDict, total=False):
2138721412
encryption_spec: Optional[genai_types.EncryptionSpec]
2138821413
"""Customer-managed encryption key spec for a prompt dataset. If set, this prompt dataset and all sub-resources of this prompt dataset will be secured by this key."""
2138921414

21415+
max_wait_time: Optional[int]
21416+
"""The maximum interval between polling requests in seconds. If not set, the default interval is 60 seconds."""
21417+
2139021418

2139121419
UpdatePromptConfigOrDict = Union[UpdatePromptConfig, UpdatePromptConfigDict]
2139221420

@@ -26388,6 +26416,10 @@ class CreatePromptConfig(_common.BaseModel):
2638826416
default=None,
2638926417
description="""The display name for the prompt version. If not set, a default name with a timestamp will be used.""",
2639026418
)
26419+
max_wait_time: Optional[int] = Field(
26420+
default=60,
26421+
description="""The maximum interval between requests in seconds. If not set, the default interval is 60 seconds.""",
26422+
)
2639126423

2639226424

2639326425
class CreatePromptConfigDict(TypedDict, total=False):
@@ -26408,6 +26440,9 @@ class CreatePromptConfigDict(TypedDict, total=False):
2640826440
version_display_name: Optional[str]
2640926441
"""The display name for the prompt version. If not set, a default name with a timestamp will be used."""
2641026442

26443+
max_wait_time: Optional[int]
26444+
"""The maximum interval between requests in seconds. If not set, the default interval is 60 seconds."""
26445+
2641126446

2641226447
CreatePromptConfigOrDict = Union[CreatePromptConfig, CreatePromptConfigDict]
2641326448

@@ -26434,6 +26469,10 @@ class CreatePromptVersionConfig(_common.BaseModel):
2643426469
default=None,
2643526470
description="""Customer-managed encryption key spec for a prompt dataset. If set, this prompt dataset and all sub-resources of this prompt dataset will be secured by this key.""",
2643626471
)
26472+
max_wait_time: Optional[int] = Field(
26473+
default=60,
26474+
description="""The maximum interval between requests in seconds. If not set, the default interval is 60 seconds.""",
26475+
)
2643726476

2643826477

2643926478
class CreatePromptVersionConfigDict(TypedDict, total=False):
@@ -26454,6 +26493,9 @@ class CreatePromptVersionConfigDict(TypedDict, total=False):
2645426493
encryption_spec: Optional[genai_types.EncryptionSpec]
2645526494
"""Customer-managed encryption key spec for a prompt dataset. If set, this prompt dataset and all sub-resources of this prompt dataset will be secured by this key."""
2645626495

26496+
max_wait_time: Optional[int]
26497+
"""The maximum interval between requests in seconds. If not set, the default interval is 60 seconds."""
26498+
2645726499

2645826500
CreatePromptVersionConfigOrDict = Union[
2645926501
CreatePromptVersionConfig, CreatePromptVersionConfigDict

0 commit comments

Comments
 (0)