Skip to content

Commit b54e39d

Browse files
CoreyEWoodAuto-format Bot
andauthored
Add request_timeout as a param for submit_image_query (#369)
Co-authored-by: Auto-format Bot <[email protected]>
1 parent a5a92ad commit b54e39d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/groundlight/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
637637
inspection_id: Optional[str] = None,
638638
metadata: Union[dict, str, None] = None,
639639
image_query_id: Optional[str] = None,
640+
request_timeout: Optional[float] = None,
640641
) -> ImageQuery:
641642
"""
642643
Evaluates an image with Groundlight. This is the core method for getting predictions about images.
@@ -718,6 +719,8 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
718719
:param image_query_id: The ID for the image query. This is to enable specific functionality
719720
and is not intended for general external use. If not set, a random ID
720721
will be generated.
722+
:param request_timeout: The total request timeout for the image query submission API request. Most users will
723+
not need to modify this. If not set, the default value will be used.
721724
722725
:return: ImageQuery with query details and result (if wait > 0)
723726
:raises ValueError: If wait > 0 when want_async=True
@@ -731,7 +734,11 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
731734

732735
image_bytesio: ByteStreamWrapper = parse_supported_image_types(image)
733736

734-
params = {"detector_id": detector_id, "body": image_bytesio, "_request_timeout": DEFAULT_REQUEST_TIMEOUT}
737+
params = {
738+
"detector_id": detector_id,
739+
"body": image_bytesio,
740+
"_request_timeout": request_timeout if request_timeout is not None else DEFAULT_REQUEST_TIMEOUT,
741+
}
735742

736743
if patience_time is not None:
737744
params["patience_time"] = patience_time

test/integration/test_groundlight.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
PaginatedDetectorList,
2828
PaginatedImageQueryList,
2929
)
30+
from urllib3.exceptions import ReadTimeoutError
3031

3132
DEFAULT_CONFIDENCE_THRESHOLD = 0.9
3233
IQ_IMPROVEMENT_THRESHOLD = 0.75
@@ -349,6 +350,17 @@ def test_submit_image_query_with_human_review_param(gl: Groundlight, detector: D
349350
assert is_valid_display_result(_image_query.result)
350351

351352

353+
def test_submit_image_query_with_low_request_timeout(gl: Groundlight, detector: Detector, image: str):
354+
"""
355+
Test that submit_image_query respects the request_timeout parameter and raises a ReadTimeoutError when timeout is
356+
exceeded.
357+
"""
358+
with pytest.raises(ReadTimeoutError):
359+
# Setting a very low request_timeout value should result in a timeout.
360+
# NOTE: request_timeout=0 seems to have special behavior that does not result in a timeout.
361+
gl.submit_image_query(detector=detector, image=image, human_review="NEVER", request_timeout=1e-8)
362+
363+
352364
@pytest.mark.skip_for_edge_endpoint(reason="The edge-endpoint does not support passing detector metadata.")
353365
def test_create_detector_with_metadata(gl: Groundlight):
354366
name = f"Test {datetime.utcnow()}" # Need a unique name

0 commit comments

Comments
 (0)