-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize pydantic and other tricky objects correctly from Rust. (#1392)
From the Rust bindings, import the `_serialize_json` function from `langsmith._internal._serde`, then use it as the default fallback if `orjson` serialization can't handle some object. This makes the Rust serialization code equivalent to the `_orjson.dumps()` call inside `langsmith._internal._serde.dumps_json`. I will handle UTF surrogate characters in a subsequent PR.
- Loading branch information
1 parent
4864506
commit 89733fb
Showing
3 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,59 @@ | ||
use std::ptr::NonNull; | ||
|
||
use pyo3::types::PyAnyMethods as _; | ||
|
||
mod writer; | ||
|
||
thread_local! { | ||
static ORJSON_DEFAULT: Result<NonNull<pyo3_ffi::PyObject>, String> = { | ||
pyo3::Python::with_gil(|py| { | ||
let module = match py.import("langsmith._internal._serde") { | ||
Ok(m) => m, | ||
Err(e) => { | ||
let _ = py.import("langsmith").map_err(|_| "failed to import `langsmith` package; please make sure `langsmith-pyo3` is only used via the `langsmith` package".to_string())?; | ||
return Err(format!("Failed to import `langsmith._internal._serde` even though `langsmith` can be imported. Did internal `langsmith` package structure change? Underlying error: {e}")); | ||
} | ||
}; | ||
|
||
let function = module.getattr("_serialize_json").map_err(|e| format!("`_serialize_json` function not found; underlying error: {e}"))?.as_ptr(); | ||
Ok(NonNull::new(function).expect("function was null, which shouldn't ever happen")) | ||
}) | ||
} | ||
} | ||
|
||
/// Perform a runtime check that we've successfully located the `langsmith` Python code | ||
/// used to transform Python objects which aren't natively serializeable by `orjson`. | ||
/// | ||
/// This assertion ensures that we won't later fail to serialize e.g. Pydantic objects. | ||
/// | ||
/// The cost of this call is trivial: just one easily branch-predictable comparison on | ||
/// an already-initialized thread-local. | ||
pub(crate) fn assert_orjson_default_is_present() { | ||
ORJSON_DEFAULT.with(|res| { | ||
if let Err(e) = res { | ||
panic!("{e}"); | ||
} | ||
}) | ||
} | ||
|
||
pub(crate) fn dumps(ptr: *mut pyo3_ffi::PyObject) -> Result<Vec<u8>, String> { | ||
let mut writer = writer::BufWriter::new(); | ||
|
||
let obj = orjson::PyObjectSerializer::new( | ||
ptr, | ||
orjson::SerializerState::new(Default::default()), | ||
None, | ||
); | ||
|
||
let res = orjson::to_writer(&mut writer, &obj); | ||
match res { | ||
Ok(_) => Ok(writer.finish()), | ||
Err(err) => { | ||
// Make sure we drop the allocated buffer. | ||
let _ = writer.into_inner(); | ||
Err(err.to_string()) | ||
ORJSON_DEFAULT.with(|default| { | ||
let obj = orjson::PyObjectSerializer::new( | ||
ptr, | ||
orjson::SerializerState::new(Default::default()), | ||
default.as_ref().cloned().ok(), | ||
); | ||
|
||
let res = orjson::to_writer(&mut writer, &obj); | ||
match res { | ||
Ok(_) => Ok(writer.finish()), | ||
Err(err) => { | ||
// Make sure we drop the allocated buffer. | ||
let _ = writer.into_inner(); | ||
Err(err.to_string()) | ||
} | ||
} | ||
} | ||
}) | ||
} |