Skip to content

Commit 427ca9a

Browse files
authored
chore: no need for explicit Python::with_gil (#4075)
Signed-off-by: PeaBrane <[email protected]>
1 parent 17af7a0 commit 427ca9a

File tree

1 file changed

+33
-48
lines changed
  • lib/bindings/python/rust/llm

1 file changed

+33
-48
lines changed

lib/bindings/python/rust/llm/kv.rs

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,47 +1115,36 @@ impl KvPushRouter {
11151115
extra_args: Option<PyObject>,
11161116
) -> PyResult<Bound<'p, PyAny>> {
11171117
// Depythonize the options with defaults
1118-
let (stop_conditions, sampling_options, output_options, router_config_override, extra_args) =
1119-
Python::with_gil(|py| {
1120-
let stop_conditions: StopConditions = if let Some(obj) = stop_conditions {
1121-
depythonize(obj.bind(py)).map_err(to_pyerr)?
1122-
} else {
1123-
StopConditions::default()
1124-
};
1118+
let stop_conditions: StopConditions = if let Some(obj) = stop_conditions {
1119+
depythonize(obj.bind(py)).map_err(to_pyerr)?
1120+
} else {
1121+
StopConditions::default()
1122+
};
11251123

1126-
let sampling_options: SamplingOptions = if let Some(obj) = sampling_options {
1127-
depythonize(obj.bind(py)).map_err(to_pyerr)?
1128-
} else {
1129-
SamplingOptions::default()
1130-
};
1124+
let sampling_options: SamplingOptions = if let Some(obj) = sampling_options {
1125+
depythonize(obj.bind(py)).map_err(to_pyerr)?
1126+
} else {
1127+
SamplingOptions::default()
1128+
};
11311129

1132-
let output_options: OutputOptions = if let Some(obj) = output_options {
1133-
depythonize(obj.bind(py)).map_err(to_pyerr)?
1134-
} else {
1135-
OutputOptions::default()
1136-
};
1130+
let output_options: OutputOptions = if let Some(obj) = output_options {
1131+
depythonize(obj.bind(py)).map_err(to_pyerr)?
1132+
} else {
1133+
OutputOptions::default()
1134+
};
11371135

1138-
let router_config_override: Option<llm_rs::kv_router::RouterConfigOverride> =
1139-
if let Some(obj) = router_config_override {
1140-
Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
1141-
} else {
1142-
None
1143-
};
1144-
1145-
let extra_args: Option<serde_json::Value> = if let Some(obj) = extra_args {
1146-
Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
1147-
} else {
1148-
None
1149-
};
1136+
let router_config_override: Option<llm_rs::kv_router::RouterConfigOverride> =
1137+
if let Some(obj) = router_config_override {
1138+
Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
1139+
} else {
1140+
None
1141+
};
11501142

1151-
Ok::<_, PyErr>((
1152-
stop_conditions,
1153-
sampling_options,
1154-
output_options,
1155-
router_config_override,
1156-
extra_args,
1157-
))
1158-
})?;
1143+
let extra_args: Option<serde_json::Value> = if let Some(obj) = extra_args {
1144+
Some(depythonize(obj.bind(py)).map_err(to_pyerr)?)
1145+
} else {
1146+
None
1147+
};
11591148

11601149
// Build the PreprocessedRequest
11611150
let mut request_builder =
@@ -1188,7 +1177,7 @@ impl KvPushRouter {
11881177
) -> PyResult<Bound<'p, PyAny>> {
11891178
// Depythonize the request directly into PreprocessedRequest
11901179
let request: llm_rs::protocols::common::preprocessor::PreprocessedRequest =
1191-
Python::with_gil(|py| depythonize(request.bind(py)).map_err(to_pyerr))?;
1180+
depythonize(request.bind(py)).map_err(to_pyerr)?;
11921181

11931182
// Use the helper method to process the request
11941183
Self::process_request_to_stream(py, self.inner.clone(), request)
@@ -1203,11 +1192,9 @@ impl KvPushRouter {
12031192
request_id: Option<String>,
12041193
) -> PyResult<Bound<'p, PyAny>> {
12051194
let router_config_override = if let Some(obj) = router_config_override {
1206-
Python::with_gil(|py| {
1207-
let override_config: llm_rs::kv_router::RouterConfigOverride =
1208-
depythonize(obj.bind(py)).map_err(to_pyerr)?;
1209-
Ok::<_, PyErr>(Some(override_config))
1210-
})?
1195+
let override_config: llm_rs::kv_router::RouterConfigOverride =
1196+
depythonize(obj.bind(py)).map_err(to_pyerr)?;
1197+
Some(override_config)
12111198
} else {
12121199
None
12131200
};
@@ -1250,11 +1237,9 @@ impl KvPushRouter {
12501237
)?;
12511238

12521239
let router_config_override = if let Some(obj) = router_config_override {
1253-
Python::with_gil(|py| {
1254-
let override_config: llm_rs::kv_router::RouterConfigOverride =
1255-
depythonize(obj.bind(py)).map_err(to_pyerr)?;
1256-
Ok::<_, PyErr>(Some(override_config))
1257-
})?
1240+
let override_config: llm_rs::kv_router::RouterConfigOverride =
1241+
depythonize(obj.bind(py)).map_err(to_pyerr)?;
1242+
Some(override_config)
12581243
} else {
12591244
None
12601245
};

0 commit comments

Comments
 (0)