# 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/4fe9d3062f4b68e2e472eb92decf369c93aebb46/libs/core/langchain_core/utils/utils.py#L509)