mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-31 10:23:18 +00:00
core[patch]: Add **kwargs to Langchain's dumps() to allow passing of json.dumps() … (#10628)
…parameters. In Langchain's `dumps()` function, I've added a `**kwargs` parameter. This allows users to pass additional parameters to the underlying `json.dumps()` function, providing greater flexibility and control over JSON serialization. Many parameters available in `json.dumps()` can be useful or even necessary in specific situations. For example, when using an Agent with return_intermediate_steps set to true, the output is a list of AgentAction objects. These objects can't be serialized without using Langchain's `dumps()` function. The issue arises when using the Agent with a language other than English, which may contain non-ASCII characters like 'é'. The default behavior of `json.dumps()` sets ensure_ascii to true, converting `{"name": "José"}` into `{"name": "Jos\u00e9"}`. This can make the output hard to read, especially in the case of intermediate steps in agent logs. By allowing users to pass additional parameters to `json.dumps()` via Langchain's dumps(), we can solve this problem. For instance, users can set `ensure_ascii=False` to maintain the original characters. This update also enables users to pass other useful `json.dumps()` parameters like `sort_keys`, providing even more flexibility. The implementation takes into account edge cases where a user might pass a "default" parameter, which is already defined by `dumps()`, or an "indent" parameter, which is also predefined if `pretty=True` is set. --------- Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -13,12 +13,15 @@ def default(obj: Any) -> Any:
|
||||
return to_json_not_implemented(obj)
|
||||
|
||||
|
||||
def dumps(obj: Any, *, pretty: bool = False) -> str:
|
||||
def dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str:
|
||||
"""Return a json string representation of an object."""
|
||||
if "default" in kwargs:
|
||||
raise ValueError("`default` should not be passed to dumps")
|
||||
if pretty:
|
||||
return json.dumps(obj, default=default, indent=2)
|
||||
indent = kwargs.pop("indent", 2)
|
||||
return json.dumps(obj, default=default, indent=indent, **kwargs)
|
||||
else:
|
||||
return json.dumps(obj, default=default)
|
||||
return json.dumps(obj, default=default, **kwargs)
|
||||
|
||||
|
||||
def dumpd(obj: Any) -> Dict[str, Any]:
|
||||
|
@@ -57,6 +57,9 @@
|
||||
}
|
||||
'''
|
||||
# ---
|
||||
# name: test_person_with_kwargs
|
||||
'{"lc":1,"type":"constructor","id":["tests","unit_tests","load","test_dump","Person"],"kwargs":{"secret":{"lc":1,"type":"secret","id":["SECRET"]},"you_can_see_me":"hello"}}'
|
||||
# ---
|
||||
# name: test_serialize_llmchain
|
||||
'''
|
||||
{
|
||||
|
@@ -141,3 +141,14 @@ def test_serialize_llmchain_with_non_serializable_arg(snapshot: Any) -> None:
|
||||
prompt = PromptTemplate.from_template("hello {name}!")
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
assert dumps(chain, pretty=True) == snapshot
|
||||
|
||||
|
||||
def test_person_with_kwargs(snapshot: Any) -> None:
|
||||
person = Person(secret="hello")
|
||||
assert dumps(person, separators=(",", ":")) == snapshot
|
||||
|
||||
|
||||
def test_person_with_invalid_kwargs() -> None:
|
||||
person = Person(secret="hello")
|
||||
with pytest.raises(TypeError):
|
||||
dumps(person, invalid_kwarg="hello")
|
||||
|
Reference in New Issue
Block a user