> ## Documentation Index
> Fetch the complete documentation index at: https://www.getmaxim.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# langchain.Utils

> Utility functions and helpers for Langchain integration.

[View module source on GitHub](https://github.com/maximhq/maxim-py/blob/main/maxim/logger/langchain/utils.py)

#### parse\_langchain\_provider

```python theme={null}
def parse_langchain_provider(serialized: Dict[str, Any])
```

Parses langchain provider from serialized data

**Arguments**:

| Name         | Description                                             |
| ------------ | ------------------------------------------------------- |
| `serialized` | Dict\[str, Any]: Serialized data to parse provider from |

**Returns**:

| Name  | Description     |
| ----- | --------------- |
| `str` | Parsed provider |

#### parse\_langchain\_llm\_error

```python theme={null}
def parse_langchain_llm_error(
    error: Union[Exception, BaseException,
                 KeyboardInterrupt]) -> GenerationError
```

Parses langchain LLM error into a format that is accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name    | Description                                                                                      |
| ------- | ------------------------------------------------------------------------------------------------ |
| `error` | Union\[Exception, KeyboardInterrupt]: [Error](/sdk/python/references/models/prompt) to be parsed |

**Returns**:

| Name                                                                | Description      |
| ------------------------------------------------------------------- | ---------------- |
| `[GenerationError](/sdk/python/references/logger/components/types)` | Parsed LLM error |

#### parse\_langchain\_model\_parameters

```python theme={null}
def parse_langchain_model_parameters(**kwargs: Any
                                     ) -> Tuple[str, Dict[str, Any]]
```

Parses langchain kwargs into model and model parameters. You can use this function with any langchain \_start callback function

**Arguments**:

| Name     | Description                          |
| -------- | ------------------------------------ |
| `kwargs` | Dict\[str, Any]: Kwargs to be parsed |

**Returns**:

Tuple\[str, Dict\[str, Any]]: Model and model parameters

**Raises**:

* `Exception` - If model\_name is not found in kwargs

#### parse\_base\_message\_to\_maxim\_generation

```python theme={null}
def parse_base_message_to_maxim_generation(message: BaseMessage)
```

Parses langchain BaseMessage into a format that is accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name      | Description |
| --------- | ----------- |
| `message` | BaseMessage |

**Returns**:

Dict\[str, Any]: Parsed message

#### parse\_langchain\_message

```python theme={null}
def parse_langchain_message(message: BaseMessage)
```

Parses langchain BaseMessage into a choice of openai message

**Arguments**:

| Name      | Description |
| --------- | ----------- |
| `message` | BaseMessage |

**Returns**:

Dict\[str, Any]: Parsed message

#### parse\_langchain\_generation

```python theme={null}
def parse_langchain_generation(generation: Generation)
```

Parses langchain generation into a format that is accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name         | Description                                                                                                                                         |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `generation` | \[[Generation](/sdk/python/references/logger/components/generation)]\(/sdk/python/references/logger/components/generation): Generation to be parsed |

**Returns**:

Dict\[str, Any]: Parsed generation

#### parse\_token\_usage\_for\_result

```python theme={null}
def parse_token_usage_for_result(result: LLMResult)
```

Parses token usage for a given LLM result

#### parse\_langchain\_chat\_result

```python theme={null}
def parse_langchain_chat_result(result: ChatResult) -> Dict[str, Any]
```

Parses langchain Chat result into a format that is accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name     | Description                          |
| -------- | ------------------------------------ |
| `result` | ChatResult: Chat result to be parsed |

**Returns**:

Dict\[str, Any]: Parsed Chat result

**Raises**:

* `Exception` - If error parsing Chat result

#### parse\_langchain\_llm\_result

```python theme={null}
def parse_langchain_llm_result(result: LLMResult) -> Dict[str, Any]
```

Parses langchain LLM result into a format that is accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name     | Description                        |
| -------- | ---------------------------------- |
| `result` | LLMResult: LLM result to be parsed |

**Returns**:

Dict\[str, Any]: Parsed LLM result

**Raises**:

* `Exception` - If error parsing LLM result

#### parse\_langchain\_messages

```python theme={null}
def parse_langchain_messages(input: Union[List[str], List[List[Any]]],
                             default_role="user")
```

Parses langchain messages into messages that are accepted by [Maxim](/sdk/python/references/maxim) logger

**Arguments**:

| Name           | Description                                                    |
| -------------- | -------------------------------------------------------------- |
| `input`        | List\[str] or List\[List\[Any]]: List of messages to be parsed |
| `default_role` | str: Default role to assign to messages without a role         |

**Returns**:

List\[Dict\[str, str]]: List of messages with role and content

**Raises**:

* `Exception` - If input is not List\[str] or List\[List\[Any]]
* `Exception` - If message type is not str or list
* `Exception` - If message type is not recognized
