> ## Documentation Index
> Fetch the complete documentation index at: https://langchat.neurobrains.co/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatResponse

> The typed return value from chat() and chat_sync().

## Class: `ChatResponse`

A dataclass returned by every `chat()` and `chat_sync()` call. Provides typed access to the response and metadata.

```python theme={null}
from langchat import ChatResponse
```

***

## Fields

<ParamField path="text" type="str">
  The AI's response text. This is what you display to the user.
</ParamField>

<ParamField path="status" type="&#x22;success&#x22; | &#x22;error&#x22;">
  Whether the request succeeded. `"success"` means the LLM returned a valid response. `"error"` means something went wrong (LLM failure, timeout, etc.).
</ParamField>

<ParamField path="user_id" type="str">
  The `user_id` you passed to `chat()`. Echoed back for convenience.
</ParamField>

<ParamField path="platform" type="str">
  The `platform` you passed to `chat()`. Defaults to `"default"`.
</ParamField>

<ParamField path="response_time" type="float">
  End-to-end latency in seconds, from when `chat()` was called to when the response was ready.
</ParamField>

<ParamField path="timestamp" type="str">
  UTC timestamp of the response in ISO 8601 format (e.g. `"2025-01-15T10:30:00.000Z"`).
</ParamField>

<ParamField path="error" type="str | None">
  Error message when `status == "error"`. `None` on success.
</ParamField>

***

## Special methods

### `__bool__`

`if response:` returns `True` when `status == "success"`.

```python theme={null}
response = await lc.chat(query="Hello", user_id="alice")

if response:
    print("Success!")
else:
    print("Failed.")
```

### `__str__`

`print(response)` and `str(response)` return `response.text`.

```python theme={null}
response = await lc.chat(query="Hello", user_id="alice")
print(response)          # prints the AI response text
```

***

## Usage patterns

### Basic

```python theme={null}
response = await lc.chat(query="What is RAG?", user_id="alice")
print(response)
```

### With error handling

```python theme={null}
response = await lc.chat(query="Explain quantum computing", user_id="alice")

if response:
    print(response.text)
    print(f"Answered in {response.response_time:.2f}s")
else:
    # Log the error and show a user-friendly message
    logger.error(f"Chat failed: {response.error}")
    print("Sorry, something went wrong. Please try again.")
```

### In an API response

```python theme={null}
from fastapi import FastAPI
from langchat import LangChat, ChatResponse

app = FastAPI()

@app.post("/ask")
async def ask(query: str, user_id: str):
    response: ChatResponse = await lc.chat(query=query, user_id=user_id)
    return {
        "answer": response.text,
        "ok": bool(response),
        "latency": response.response_time,
        "timestamp": response.timestamp,
    }
```

### In a chat loop

```python theme={null}
async def chat_loop(user_id: str):
    while True:
        query = input("You: ")
        if not query.strip():
            continue

        response = await lc.chat(query=query, user_id=user_id)

        if response:
            print(f"Bot: {response.text}")
        else:
            print(f"Error: {response.error}")
```
