|
| 1 | +"""Custom extract patch installed on top of the session helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any, cast |
| 6 | + |
| 7 | +import httpx |
| 8 | +from typing_extensions import Unpack |
| 9 | + |
| 10 | +from ._pydantic_extract import is_pydantic_model, pydantic_model_to_json_schema, validate_extract_response |
| 11 | +from .session import AsyncSession, Session |
| 12 | +from ._types import Body, Headers, NotGiven, Query, not_given |
| 13 | +from .types import session_extract_params |
| 14 | +from .types.session_extract_response import SessionExtractResponse |
| 15 | + |
| 16 | +_ORIGINAL_SESSION_EXTRACT = Session.extract |
| 17 | +_ORIGINAL_ASYNC_SESSION_EXTRACT = AsyncSession.extract |
| 18 | + |
| 19 | + |
| 20 | +def install_pydantic_extract_patch() -> None: |
| 21 | + if getattr(Session.extract, "__stagehand_pydantic_extract_patch__", False): |
| 22 | + return |
| 23 | + |
| 24 | + Session.extract = _sync_extract # type: ignore[assignment] |
| 25 | + AsyncSession.extract = _async_extract # type: ignore[assignment] |
| 26 | + |
| 27 | + |
| 28 | +def _sync_extract( # type: ignore[override, misc] |
| 29 | + self: Session, |
| 30 | + *, |
| 31 | + schema: dict[str, object] | type | None = None, |
| 32 | + page: Any | None = None, |
| 33 | + extra_headers: Headers | None = None, |
| 34 | + extra_query: Query | None = None, |
| 35 | + extra_body: Body | None = None, |
| 36 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 37 | + **params: Unpack[session_extract_params.SessionExtractParamsNonStreaming], # pyright: ignore[reportGeneralTypeIssues] |
| 38 | +) -> SessionExtractResponse: |
| 39 | + params_schema = params.pop("schema", None) # type: ignore[misc] |
| 40 | + resolved_schema = schema if schema is not None else params_schema |
| 41 | + |
| 42 | + pydantic_cls: type[Any] | None = None |
| 43 | + if is_pydantic_model(resolved_schema): |
| 44 | + pydantic_cls = resolved_schema # type: ignore[assignment] |
| 45 | + resolved_schema = pydantic_model_to_json_schema(pydantic_cls) # type: ignore[arg-type] |
| 46 | + |
| 47 | + response = _ORIGINAL_SESSION_EXTRACT( |
| 48 | + self, |
| 49 | + page=page, |
| 50 | + extra_headers=extra_headers, |
| 51 | + extra_query=extra_query, |
| 52 | + extra_body=extra_body, |
| 53 | + timeout=timeout, |
| 54 | + **_with_schema(params, resolved_schema), |
| 55 | + ) |
| 56 | + |
| 57 | + if pydantic_cls is not None and response.data and response.data.result is not None: |
| 58 | + response.data.result = validate_extract_response( |
| 59 | + response.data.result, |
| 60 | + pydantic_cls, |
| 61 | + strict_response_validation=self._client._strict_response_validation, |
| 62 | + ) |
| 63 | + |
| 64 | + return response |
| 65 | + |
| 66 | + |
| 67 | +async def _async_extract( # type: ignore[override, misc] |
| 68 | + self: AsyncSession, |
| 69 | + *, |
| 70 | + schema: dict[str, object] | type | None = None, |
| 71 | + page: Any | None = None, |
| 72 | + extra_headers: Headers | None = None, |
| 73 | + extra_query: Query | None = None, |
| 74 | + extra_body: Body | None = None, |
| 75 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 76 | + **params: Unpack[session_extract_params.SessionExtractParamsNonStreaming], # pyright: ignore[reportGeneralTypeIssues] |
| 77 | +) -> SessionExtractResponse: |
| 78 | + params_schema = params.pop("schema", None) # type: ignore[misc] |
| 79 | + resolved_schema = schema if schema is not None else params_schema |
| 80 | + |
| 81 | + pydantic_cls: type[Any] | None = None |
| 82 | + if is_pydantic_model(resolved_schema): |
| 83 | + pydantic_cls = resolved_schema # type: ignore[assignment] |
| 84 | + resolved_schema = pydantic_model_to_json_schema(pydantic_cls) # type: ignore[arg-type] |
| 85 | + |
| 86 | + response = await _ORIGINAL_ASYNC_SESSION_EXTRACT( |
| 87 | + self, |
| 88 | + page=page, |
| 89 | + extra_headers=extra_headers, |
| 90 | + extra_query=extra_query, |
| 91 | + extra_body=extra_body, |
| 92 | + timeout=timeout, |
| 93 | + **_with_schema(params, resolved_schema), |
| 94 | + ) |
| 95 | + |
| 96 | + if pydantic_cls is not None and response.data and response.data.result is not None: |
| 97 | + response.data.result = validate_extract_response( |
| 98 | + response.data.result, |
| 99 | + pydantic_cls, |
| 100 | + strict_response_validation=self._client._strict_response_validation, |
| 101 | + ) |
| 102 | + |
| 103 | + return response |
| 104 | + |
| 105 | + |
| 106 | +def _with_schema( |
| 107 | + params: session_extract_params.SessionExtractParamsNonStreaming, |
| 108 | + schema: dict[str, object] | type | None, |
| 109 | +) -> session_extract_params.SessionExtractParamsNonStreaming: |
| 110 | + api_params = dict(params) |
| 111 | + if schema is not None: |
| 112 | + api_params["schema"] = cast(Any, schema) |
| 113 | + return cast(session_extract_params.SessionExtractParamsNonStreaming, api_params) |
| 114 | + |
| 115 | + |
| 116 | +_sync_extract.__module__ = _ORIGINAL_SESSION_EXTRACT.__module__ |
| 117 | +_sync_extract.__name__ = _ORIGINAL_SESSION_EXTRACT.__name__ |
| 118 | +_sync_extract.__qualname__ = _ORIGINAL_SESSION_EXTRACT.__qualname__ |
| 119 | +_sync_extract.__doc__ = _ORIGINAL_SESSION_EXTRACT.__doc__ |
| 120 | +_sync_extract.__stagehand_pydantic_extract_patch__ = True |
| 121 | + |
| 122 | +_async_extract.__module__ = _ORIGINAL_ASYNC_SESSION_EXTRACT.__module__ |
| 123 | +_async_extract.__name__ = _ORIGINAL_ASYNC_SESSION_EXTRACT.__name__ |
| 124 | +_async_extract.__qualname__ = _ORIGINAL_ASYNC_SESSION_EXTRACT.__qualname__ |
| 125 | +_async_extract.__doc__ = _ORIGINAL_ASYNC_SESSION_EXTRACT.__doc__ |
| 126 | +_async_extract.__stagehand_pydantic_extract_patch__ = True |
0 commit comments