Home / Function/ _is_field_useful() — langchain Function Reference

_is_field_useful() — langchain Function Reference

Architecture documentation for the _is_field_useful() function in serializable.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  91fcdb70_8569_6a1c_ff2f_a89e585ee5a9["_is_field_useful()"]
  b5d5ce95_4e41_41ed_7fe6_1a936c2c18f4["serializable.py"]
  91fcdb70_8569_6a1c_ff2f_a89e585ee5a9 -->|defined in| b5d5ce95_4e41_41ed_7fe6_1a936c2c18f4
  1d0f0a19_7194_6a37_5750_0e64e020c9c4["to_json()"]
  1d0f0a19_7194_6a37_5750_0e64e020c9c4 -->|calls| 91fcdb70_8569_6a1c_ff2f_a89e585ee5a9
  bf3a128e_3727_0d31_7547_5332e3b7613a["_try_neq_default()"]
  91fcdb70_8569_6a1c_ff2f_a89e585ee5a9 -->|calls| bf3a128e_3727_0d31_7547_5332e3b7613a
  style 91fcdb70_8569_6a1c_ff2f_a89e585ee5a9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/load/serializable.py lines 283–325

def _is_field_useful(inst: Serializable, key: str, value: Any) -> bool:
    """Check if a field is useful as a constructor argument.

    Args:
        inst: The instance.
        key: The key.
        value: The value.

    Returns:
        Whether the field is useful. If the field is required, it is useful.
        If the field is not required, it is useful if the value is not `None`.
        If the field is not required and the value is `None`, it is useful if the
        default value is different from the value.
    """
    field = type(inst).model_fields.get(key)
    if not field:
        return False

    if field.is_required():
        return True

    # Handle edge case: a value cannot be converted to a boolean (e.g. a
    # Pandas DataFrame).
    try:
        value_is_truthy = bool(value)
    except Exception as _:
        value_is_truthy = False

    if value_is_truthy:
        return True

    # Value is still falsy here!
    if field.default_factory is dict and isinstance(value, dict):
        return False

    # Value is still falsy here!
    if field.default_factory is list and isinstance(value, list):
        return False

    value_neq_default = _try_neq_default(value, field)

    # If value is falsy and does not match the default
    return value_is_truthy or value_neq_default

Subdomains

Called By

Frequently Asked Questions

What does _is_field_useful() do?
_is_field_useful() is a function in the langchain codebase, defined in libs/core/langchain_core/load/serializable.py.
Where is _is_field_useful() defined?
_is_field_useful() is defined in libs/core/langchain_core/load/serializable.py at line 283.
What does _is_field_useful() call?
_is_field_useful() calls 1 function(s): _try_neq_default.
What calls _is_field_useful()?
_is_field_useful() is called by 1 function(s): to_json.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free