# ensure_id

> **Function** in `langchain_core`

📖 [View in docs](https://reference.langchain.com/python/langchain-core/utils/utils/ensure_id)

Ensure the ID is a valid string, generating a new UUID if not provided.

Auto-generated UUIDs are prefixed by `'lc_'` to indicate they are
LangChain-generated IDs.

## Signature

```python
ensure_id(
    id_val: str | None,
) -> str
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `id_val` | `str \| None` | Yes | Optional string ID value to validate. |

## Returns

`str`

A string ID, either the validated provided value or a newly generated UUID4.

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/6fb37dba71da807af60aa7b909f71f0625a666bf/libs/core/langchain_core/utils/utils.py#L509)