@@ -301,46 +301,6 @@ fn make_dummy_client() -> Client {
301301
302302#[ pymethods]
303303impl BaseTensorZeroGateway {
304- #[ new]
305- #[ pyo3( signature = ( base_url, * , timeout=None , verbose_errors=false ) ) ]
306- fn new (
307- py : Python < ' _ > ,
308- base_url : & str ,
309- timeout : Option < f64 > ,
310- verbose_errors : bool ,
311- ) -> PyResult < Self > {
312- let mut client_builder = ClientBuilder :: new ( ClientBuilderMode :: HTTPGateway {
313- url : Url :: parse ( base_url)
314- . map_err ( |e| PyValueError :: new_err ( format ! ( "Failed to parse base_url: {e:?}" ) ) ) ?,
315- } )
316- . with_verbose_errors ( verbose_errors) ;
317- if let Some ( timeout) = timeout {
318- let http_client = reqwest:: Client :: builder ( )
319- . timeout (
320- Duration :: try_from_secs_f64 ( timeout)
321- . map_err ( |e| PyValueError :: new_err ( format ! ( "Invalid timeout: {e}" ) ) ) ?,
322- )
323- . build ( )
324- . map_err ( |e| {
325- PyValueError :: new_err ( format ! ( "Failed to build HTTP client: {e:?}" ) )
326- } ) ?;
327- client_builder = client_builder. with_http_client ( http_client) ;
328- }
329- let client = match client_builder. build_http ( ) {
330- Ok ( client) => client,
331- Err ( e) => {
332- return Err ( tensorzero_core_error (
333- py,
334- & format ! ( "Failed to construct TensorZero client: {e:?}" ) ,
335- ) ?) ;
336- }
337- } ;
338-
339- Ok ( Self {
340- client : DropInTokio :: new ( client, make_dummy_client) ,
341- } )
342- }
343-
344304 #[ pyo3( signature = ( * , input, function_name=None , model_name=None , episode_id=None , stream=None , params=None , variant_name=None , dryrun=None , output_schema=None , allowed_tools=None , provider_tools=None , additional_tools=None , tool_choice=None , parallel_tool_calls=None , internal=None , tags=None , credentials=None , cache_options=None , extra_body=None , extra_headers=None , include_original_response=None , otlp_traces_extra_headers=None , internal_dynamic_variant_config=None ) ) ]
345305 #[ expect( clippy:: too_many_arguments) ]
346306 fn _prepare_inference_request (
@@ -581,47 +541,34 @@ impl BaseTensorZeroGateway {
581541}
582542#[ pymethods]
583543impl TensorZeroGateway {
584- #[ new]
585- #[ pyo3( signature = ( base_url, * , timeout=None ) ) ]
586- fn new (
587- py : Python < ' _ > ,
588- base_url : & str ,
589- timeout : Option < f64 > ,
590- ) -> PyResult < ( Self , BaseTensorZeroGateway ) > {
591- tracing:: warn!( "TensorZeroGateway.__init__ is deprecated. Use TensorZeroGateway.build_http or TensorZeroGateway.build_embedded instead." ) ;
592- Ok ( (
593- Self { } ,
594- BaseTensorZeroGateway :: new ( py, base_url, timeout, false ) ?,
595- ) )
596- }
597-
598544 #[ classmethod]
599- #[ pyo3( signature = ( * , gateway_url, timeout=None , verbose_errors=false ) ) ]
545+ #[ pyo3( signature = ( * , gateway_url, timeout=None , verbose_errors=false , api_key= None ) ) ]
600546 /// Initialize the TensorZero client, using the HTTP gateway.
601547 /// :param gateway_url: The base URL of the TensorZero gateway. Example: "http://localhost:3000"
602548 /// :param timeout: The timeout for the HTTP client in seconds. If not provided, no timeout will be set.
603549 /// :param verbose_errors: If true, the client will increase the detail in errors (increasing the risk of leaking sensitive information).
550+ /// :param api_key: The API key to use for authentication with the TensorZero Gateway. If not provided, the client will attempt to read from the TENSORZERO_API_KEY environment variable.
604551 /// :return: A `TensorZeroGateway` instance configured to use the HTTP gateway.
605552 fn build_http (
606553 cls : & Bound < ' _ , PyType > ,
607554 gateway_url : & str ,
608555 timeout : Option < f64 > ,
609556 verbose_errors : bool ,
557+ api_key : Option < String > ,
610558 ) -> PyResult < Py < TensorZeroGateway > > {
611559 let mut client_builder = ClientBuilder :: new ( ClientBuilderMode :: HTTPGateway {
612560 url : Url :: parse ( gateway_url)
613561 . map_err ( |e| PyValueError :: new_err ( format ! ( "Invalid gateway URL: {e}" ) ) ) ?,
614562 } )
615563 . with_verbose_errors ( verbose_errors) ;
564+ if let Some ( api_key) = api_key {
565+ client_builder = client_builder. with_api_key ( api_key) ;
566+ }
616567 if let Some ( timeout) = timeout {
617- let http_client = reqwest:: Client :: builder ( )
618- . timeout (
619- Duration :: try_from_secs_f64 ( timeout)
620- . map_err ( |e| PyValueError :: new_err ( format ! ( "Invalid timeout: {e}" ) ) ) ?,
621- )
622- . build ( )
623- . map_err ( |e| PyValueError :: new_err ( format ! ( "Failed to build HTTP client: {e}" ) ) ) ?;
624- client_builder = client_builder. with_http_client ( http_client) ;
568+ client_builder = client_builder. with_timeout (
569+ Duration :: try_from_secs_f64 ( timeout)
570+ . map_err ( |e| PyValueError :: new_err ( format ! ( "Invalid timeout: {e}" ) ) ) ?,
571+ ) ;
625572 }
626573 let client_fut = client_builder. build ( ) ;
627574 let client_res = tokio_block_on_without_gil ( cls. py ( ) , client_fut) ;
@@ -641,18 +588,6 @@ impl TensorZeroGateway {
641588 Py :: new ( cls. py ( ) , instance)
642589 }
643590
644- /// **Deprecated** (use `build_http` or `build_embedded` instead)
645- /// Initialize the TensorZero client.
646- ///
647- /// :param base_url: The base URL of the TensorZero gateway. Example: "http://localhost:3000"
648- /// :param timeout: The timeout for the HTTP client in seconds. If not provided, no timeout will be set.
649- #[ expect( unused_variables) ]
650- #[ pyo3( signature = ( base_url, * , timeout=None ) ) ]
651- fn __init__ ( this : Py < Self > , base_url : & str , timeout : Option < f64 > ) -> Py < Self > {
652- // The actual logic is in the 'new' method - this method just exists to generate a docstring
653- this
654- }
655-
656591 /// Close the connection to the TensorZero gateway.
657592 #[ expect( clippy:: unused_self) ]
658593 fn close ( & self ) {
@@ -1391,46 +1326,33 @@ struct AsyncTensorZeroGateway {}
13911326
13921327#[ pymethods]
13931328impl AsyncTensorZeroGateway {
1394- #[ new]
1395- #[ pyo3( signature = ( base_url, * , timeout=None ) ) ]
1396- fn new (
1397- py : Python < ' _ > ,
1398- base_url : & str ,
1399- timeout : Option < f64 > ,
1400- ) -> PyResult < ( Self , BaseTensorZeroGateway ) > {
1401- tracing:: warn!( "AsyncTensorZeroGateway.__init__ is deprecated. Use AsyncTensorZeroGateway.build_http or AsyncTensorZeroGateway.build_embedded instead." ) ;
1402- Ok ( (
1403- Self { } ,
1404- BaseTensorZeroGateway :: new ( py, base_url, timeout, false ) ?,
1405- ) )
1406- }
1407-
14081329 #[ classmethod]
1409- #[ pyo3( signature = ( * , gateway_url, timeout=None , verbose_errors=false , async_setup=true ) ) ]
1330+ #[ pyo3( signature = ( * , gateway_url, timeout=None , verbose_errors=false , async_setup=true , api_key= None ) ) ]
14101331 /// Initialize the TensorZero client, using the HTTP gateway.
14111332 /// :param gateway_url: The base URL of the TensorZero gateway. Example: "http://localhost:3000"
14121333 /// :param timeout: The timeout for the HTTP client in seconds. If not provided, no timeout will be set.
14131334 /// :param verbose_errors: If true, the client will increase the detail in errors (increasing the risk of leaking sensitive information).
14141335 /// :param async_setup: If true, this method will return a `Future` that resolves to an `AsyncTensorZeroGateway` instance. Otherwise, it will block and construct the `AsyncTensorZeroGateway`
1336+ /// :param api_key: The API key to use for authentication with the TensorZero Gateway. If not provided, the client will attempt to read from the TENSORZERO_API_KEY environment variable.
14151337 /// :return: An `AsyncTensorZeroGateway` instance configured to use the HTTP gateway.
14161338 fn build_http (
14171339 cls : & Bound < ' _ , PyType > ,
14181340 gateway_url : & str ,
14191341 timeout : Option < f64 > ,
14201342 verbose_errors : bool ,
14211343 async_setup : bool ,
1344+ api_key : Option < String > ,
14221345 ) -> PyResult < Py < PyAny > > {
14231346 let mut client_builder = ClientBuilder :: new ( ClientBuilderMode :: HTTPGateway {
14241347 url : Url :: parse ( gateway_url)
14251348 . map_err ( |e| PyValueError :: new_err ( format ! ( "Invalid gateway URL: {e}" ) ) ) ?,
14261349 } )
14271350 . with_verbose_errors ( verbose_errors) ;
1351+ if let Some ( api_key) = api_key {
1352+ client_builder = client_builder. with_api_key ( api_key) ;
1353+ }
14281354 if let Some ( timeout) = timeout {
1429- let http_client = reqwest:: Client :: builder ( )
1430- . timeout ( Duration :: from_secs_f64 ( timeout) )
1431- . build ( )
1432- . map_err ( |e| PyValueError :: new_err ( format ! ( "Failed to build HTTP client: {e}" ) ) ) ?;
1433- client_builder = client_builder. with_http_client ( http_client) ;
1355+ client_builder = client_builder. with_timeout ( Duration :: from_secs_f64 ( timeout) ) ;
14341356 }
14351357 let client_fut = client_builder. build ( ) ;
14361358 let build_gateway = async move {
@@ -1463,18 +1385,6 @@ impl AsyncTensorZeroGateway {
14631385 }
14641386 }
14651387
1466- /// **Deprecated** (use `build_http` or `build_embedded` instead)
1467- /// Initialize the TensorZero client.
1468- ///
1469- /// :param base_url: The base URL of the TensorZero gateway. Example: "http://localhost:3000"
1470- /// :param timeout: The timeout for the HTTP client in seconds. If not provided, no timeout will be set.
1471- #[ expect( unused_variables) ]
1472- #[ pyo3( signature = ( base_url, * , timeout=None ) ) ]
1473- fn __init__ ( this : Py < Self > , base_url : & str , timeout : Option < f64 > ) -> Py < Self > {
1474- // The actual logic is in the 'new' method - this method just exists to generate a docstring
1475- this
1476- }
1477-
14781388 /// Close the connection to the TensorZero gateway.
14791389 async fn close ( & self ) {
14801390 // TODO - implement closing the 'reqwest' connection pool: https://github.com/tensorzero/tensorzero/issues/857
0 commit comments