# WatsonxEmbeddings

> **Class** in `langchain_ibm`

📖 [View in docs](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings)

`IBM watsonx.ai` embedding model integration.

???+ info "Setup"

    To use, you should have `langchain_ibm` python package installed,
    and the environment variable `WATSONX_API_KEY` set with your API key, or pass
    it as a named parameter `api_key` to the constructor.

    ```bash
    pip install -U langchain-ibm

    # or using uv
    uv add langchain-ibm
    ```

    ```bash
    export WATSONX_API_KEY="your-api-key"
    ```

    !!! deprecated
        `apikey` and `WATSONX_APIKEY` are deprecated and will be removed in
        version `2.0.0`. Use `api_key` and `WATSONX_API_KEY` instead.

??? info "Instantiate"

    ```python
    from langchain_ibm import WatsonxEmbeddings

    embeddings = WatsonxEmbeddings(
        model_id="ibm/granite-embedding-278m-multilingual",
        url="https://us-south.ml.cloud.ibm.com",
        project_id="*****",
        # api_key="*****"
    )
    ```

??? info "Embed single text"

    ```python
    input_text = "The meaning of life is 42"
    vector = embeddings.embed_query("hello")
    print(vector[:3])
    ```

    ```python
    [-0.0020519258, 0.0147288125, -0.0090887165]
    ```

??? info "Embed multiple texts"

    ```python
    vectors = embeddings.embed_documents(["hello", "goodbye"])
    # Showing only the first 3 coordinates
    print(len(vectors))
    print(vectors[0][:3])
    ```

    ```python
    2
    [-0.0020519265, 0.01472881, -0.009088721]
    ```

??? info "Async"

    ```python
    await embeddings.aembed_query(input_text)
    print(vector[:3])

    # multiple:
    # await embeddings.aembed_documents(input_texts)
    ```

    ```python
    [-0.0020519258, 0.0147288125, -0.0090887165]
    ```

## Signature

```python
WatsonxEmbeddings()
```

## Extends

- `BaseModel`
- `LangChainEmbeddings`

## Properties

- `model_id`
- `model`
- `project_id`
- `space_id`
- `url`
- `apikey`
- `api_key`
- `token`
- `password`
- `username`
- `instance_id`
- `version`
- `params`
- `verify`
- `watsonx_embed`
- `watsonx_embed_gateway`
- `watsonx_client`
- `model_config`

## Methods

- [`validate_environment()`](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings/validate_environment)
- [`embed_documents()`](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings/embed_documents)
- [`aembed_documents()`](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings/aembed_documents)
- [`embed_query()`](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings/embed_query)
- [`aembed_query()`](https://reference.langchain.com/python/langchain-ibm/embeddings/WatsonxEmbeddings/aembed_query)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-ibm/blob/68e9b09f8cbc8d2310e57a6a7eb51cde0956a3b0/libs/ibm/langchain_ibm/embeddings.py#L35)