to_json() — langchain Function Reference
Architecture documentation for the to_json() function in serializable.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD aecfa56d_b146_115e_0cb4_2501f187c957["to_json()"] e3623fbf_23b3_a8fc_0b7c_43b53f33b606["Serializable"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|defined in| e3623fbf_23b3_a8fc_0b7c_43b53f33b606 32debbeb_db9d_8dff_1fbc_f05613f5911c["is_lc_serializable()"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|calls| 32debbeb_db9d_8dff_1fbc_f05613f5911c a15003b1_942f_9d55_21b9_607115658104["lc_id()"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|calls| a15003b1_942f_9d55_21b9_607115658104 d2c83094_c95d_b6a5_f856_4ba32600d468["_is_field_useful()"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|calls| d2c83094_c95d_b6a5_f856_4ba32600d468 caa55af1_94f1_691e_911f_f2aedce04cba["_replace_secrets()"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|calls| caa55af1_94f1_691e_911f_f2aedce04cba 4363d6f1_ce8b_9048_d3c3_79b34c74ee3a["to_json_not_implemented()"] aecfa56d_b146_115e_0cb4_2501f187c957 -->|calls| 4363d6f1_ce8b_9048_d3c3_79b34c74ee3a style aecfa56d_b146_115e_0cb4_2501f187c957 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/load/serializable.py lines 196–272
def to_json(self) -> SerializedConstructor | SerializedNotImplemented:
"""Serialize the object to JSON.
Raises:
ValueError: If the class has deprecated attributes.
Returns:
A JSON serializable object or a `SerializedNotImplemented` object.
"""
if not self.is_lc_serializable():
return self.to_json_not_implemented()
model_fields = type(self).model_fields
secrets = {}
# Get latest values for kwargs if there is an attribute with same name
lc_kwargs = {}
for k, v in self:
if not _is_field_useful(self, k, v):
continue
# Do nothing if the field is excluded
if k in model_fields and model_fields[k].exclude:
continue
lc_kwargs[k] = getattr(self, k, v)
# Merge the lc_secrets and lc_attributes from every class in the MRO
for cls in [None, *self.__class__.mro()]:
# Once we get to Serializable, we're done
if cls is Serializable:
break
if cls:
deprecated_attributes = [
"lc_namespace",
"lc_serializable",
]
for attr in deprecated_attributes:
if hasattr(cls, attr):
msg = (
f"Class {self.__class__} has a deprecated "
f"attribute {attr}. Please use the corresponding "
f"classmethod instead."
)
raise ValueError(msg)
# Get a reference to self bound to each class in the MRO
this = cast("Serializable", self if cls is None else super(cls, self))
secrets.update(this.lc_secrets)
# Now also add the aliases for the secrets
# This ensures known secret aliases are hidden.
# Note: this does NOT hide any other extra kwargs
# that are not present in the fields.
for key in list(secrets):
value = secrets[key]
if (key in model_fields) and (
alias := model_fields[key].alias
) is not None:
secrets[alias] = value
lc_kwargs.update(this.lc_attributes)
# include all secrets, even if not specified in kwargs
# as these secrets may be passed as an environment variable instead
for key in secrets:
secret_value = getattr(self, key, None) or lc_kwargs.get(key)
if secret_value is not None:
lc_kwargs.update({key: secret_value})
return {
"lc": 1,
"type": "constructor",
"id": self.lc_id(),
"kwargs": lc_kwargs
if not secrets
else _replace_secrets(lc_kwargs, secrets),
}
Domain
Subdomains
Source
Frequently Asked Questions
What does to_json() do?
to_json() is a function in the langchain codebase, defined in libs/core/langchain_core/load/serializable.py.
Where is to_json() defined?
to_json() is defined in libs/core/langchain_core/load/serializable.py at line 196.
What does to_json() call?
to_json() calls 5 function(s): _is_field_useful, _replace_secrets, is_lc_serializable, lc_id, to_json_not_implemented.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free