Skip to content

Commit bc3e560

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 bc3e560

2 files changed

Lines changed: 110 additions & 18 deletions

File tree

agentplatform/_genai/prompts.py

Lines changed: 68 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}"
@@ -2043,7 +2062,10 @@ def restore_version(
20432062
)
20442063
self._wait_for_project_operation(
20452064
operation=restore_prompt_operation,
2046-
timeout=90,
2065+
timeout=config.timeout if config and config.timeout else 90,
2066+
max_wait_time=(
2067+
config.max_wait_time if config and config.max_wait_time else 60
2068+
),
20472069
)
20482070
dataset_version_resource = self._get_dataset_version_resource(
20492071
dataset_id=prompt_id,
@@ -2400,7 +2422,10 @@ def update(
24002422
)
24012423
dataset_version_resource_name = self._wait_for_operation(
24022424
operation=create_dataset_version_operation,
2403-
timeout=config.timeout if config else 90,
2425+
timeout=config.timeout if config and config.timeout else 90,
2426+
max_wait_time=(
2427+
config.max_wait_time if config and config.max_wait_time else 60
2428+
),
24042429
)
24052430
dataset_version_id = dataset_version_resource_name.split("/")[-1]
24062431

@@ -3558,7 +3583,10 @@ async def create(
35583583
)
35593584
dataset_resource_name = await self._wait_for_operation(
35603585
operation=create_prompt_dataset_operation,
3561-
timeout=config.timeout if config else 90,
3586+
timeout=config.timeout if config and config.timeout else 90,
3587+
max_wait_time=(
3588+
config.max_wait_time if config and config.max_wait_time else 60
3589+
),
35623590
)
35633591
dataset_id = dataset_resource_name.split("/")[-1]
35643592

@@ -3629,7 +3657,10 @@ async def create_version(
36293657
)
36303658
dataset_resource_name = await self._wait_for_operation(
36313659
operation=create_prompt_dataset_operation,
3632-
timeout=config.timeout if config else 90,
3660+
timeout=config.timeout if config and config.timeout else 90,
3661+
max_wait_time=(
3662+
config.max_wait_time if config and config.max_wait_time else 60
3663+
),
36333664
)
36343665
dataset_id = dataset_resource_name.split("/")[-1]
36353666

@@ -3653,7 +3684,10 @@ async def create_version(
36533684
)
36543685
dataset_version_resource_name = await self._wait_for_operation(
36553686
operation=create_dataset_version_operation,
3656-
timeout=config.timeout if config else 90,
3687+
timeout=config.timeout if config and config.timeout else 90,
3688+
max_wait_time=(
3689+
config.max_wait_time if config and config.max_wait_time else 60
3690+
),
36573691
)
36583692

36593693
# Step 4: Get the dataset version resource and return it with the prompt
@@ -3740,7 +3774,10 @@ async def update(
37403774
)
37413775
dataset_version_resource_name = await self._wait_for_operation(
37423776
operation=create_dataset_version_operation,
3743-
timeout=config.timeout if config else 90,
3777+
timeout=config.timeout if config and config.timeout else 90,
3778+
max_wait_time=(
3779+
config.max_wait_time if config and config.max_wait_time else 60
3780+
),
37443781
)
37453782
dataset_version_id = dataset_version_resource_name.split("/")[-1]
37463783

@@ -3760,12 +3797,14 @@ async def _wait_for_operation(
37603797
self,
37613798
operation: types.DatasetOperation,
37623799
timeout: int,
3800+
max_wait_time: int = 60,
37633801
) -> str:
37643802
"""Waits for a dataset operation to complete.
37653803
37663804
Args:
37673805
operation: The dataset operation to wait for.
37683806
timeout: The maximum time to wait for the operation to complete.
3807+
max_wait_time: The maximum interval between polling requests in seconds.
37693808
37703809
Returns:
37713810
The name of the Dataset resource from the operation result.
@@ -3787,7 +3826,6 @@ async def _wait_for_operation(
37873826
start_time = time.time()
37883827
sleep_duration = 5
37893828
wait_multiplier = 2
3790-
max_wait_time = 60
37913829
previous_time = time.time()
37923830

37933831
while not done:
@@ -3885,6 +3923,7 @@ async def _wait_for_project_operation(
38853923
self,
38863924
operation: genai_types.ProjectOperation,
38873925
timeout: int,
3926+
max_wait_time: int = 60,
38883927
) -> None:
38893928
"""Waits for a dataset deletion operation to complete.
38903929
@@ -3893,6 +3932,7 @@ async def _wait_for_project_operation(
38933932
Args:
38943933
operation: The project operation to wait for.
38953934
timeout: The maximum time to wait for the operation to complete.
3935+
max_wait_time: The maximum interval between polling requests in seconds.
38963936
Raises:
38973937
TimeoutError: If the operation does not complete within the timeout.
38983938
ValueError: If the operation fails.
@@ -3902,7 +3942,6 @@ async def _wait_for_project_operation(
39023942
start_time = time.time()
39033943
sleep_duration = 5
39043944
wait_multiplier = 2
3905-
max_wait_time = 60
39063945
previous_time = time.time()
39073946
while not done:
39083947
if (time.time() - start_time) > timeout:
@@ -3947,7 +3986,11 @@ async def delete(
39473986
config=config,
39483987
)
39493988
await self._wait_for_project_operation(
3950-
operation=delete_prompt_operation, timeout=config.timeout if config else 90
3989+
operation=delete_prompt_operation,
3990+
timeout=config.timeout if config and config.timeout else 90,
3991+
max_wait_time=(
3992+
config.max_wait_time if config and config.max_wait_time else 60
3993+
),
39513994
)
39523995
logger.info(f"Deleted prompt with id: {prompt_id}")
39533996

@@ -3975,7 +4018,11 @@ async def delete_version(
39754018
)
39764019

39774020
await self._wait_for_project_operation(
3978-
operation=delete_version_operation, timeout=config.timeout if config else 90
4021+
operation=delete_version_operation,
4022+
timeout=config.timeout if config and config.timeout else 90,
4023+
max_wait_time=(
4024+
config.max_wait_time if config and config.max_wait_time else 60
4025+
),
39794026
)
39804027
logger.info(
39814028
f"Deleted prompt version {version_id} from prompt with id: {prompt_id}"
@@ -4114,7 +4161,10 @@ async def restore_version(
41144161
)
41154162
await self._wait_for_project_operation(
41164163
operation=restore_prompt_operation,
4117-
timeout=90,
4164+
timeout=config.timeout if config and config.timeout else 90,
4165+
max_wait_time=(
4166+
config.max_wait_time if config and config.max_wait_time else 60
4167+
),
41184168
)
41194169
dataset_version_resource = await self._get_dataset_version_resource(
41204170
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)