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

# Python

> The LLMGrid Python SDK uses a unified completion() interface with OpenAI-compatible parameters and a standardized response shape

Basic chat completion

```text theme={null}

from llmgrid import completion
import os

response = completion(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "Hello from LLMGrid"}],
    api_key=os.environ.get("LLMGRID_API_KEY"),
    base_url="https://api.llmgrid.ai/v1",
)

print(response.choices[0].message.content)
```

### Common parameters

`completion()` accepts OpenAI-compatible parameters such as `model`, `messages`, `temperature`, `max_tokens`, `stream`, and `tools`.

### Streaming

Set `stream=True` to receive chunks incrementally (OpenAI-compatible streaming semantics).

```text theme={null}
from llmgrid import completion
import os

stream = completion(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "Write a 3-line poem about gateways."}],
    stream=True,
    api_key=os.environ.get("LLMGRID_API_KEY"),
    base_url="https://api.llmgrid.ai/v1",
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta and getattr(delta, "content", None):
        print(delta.content, end="")
``
```

### Tool / function calling (schema)

Tools can be passed via the `tools` parameter using OpenAI-compatible tool schemas.

```text theme={null}
from llmgrid import completion
import os

tools = [
  {
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get the weather for a city",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {"type": "string"}
        },
        "required": ["city"]
      }
    }
  }
]

resp = completion(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "What's the weather in Chicago?"}],
    tools=tools,
    api_key=os.environ.get("LLMGRID_API_KEY"),
    base_url="https://api.llmgrid.ai/v1",
)

print(resp)
``
```

## Python (OpenAI SDK pointed to LLMGrid)

The official OpenAI Python client supports configuring a custom `base_url` and reads API keys from environment variables by default.

```text theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("LLMGRID_API_KEY"),
    base_url="https://api.llmgrid.ai/v1",
)

response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[{"role": "user", "content": "Hello via OpenAI SDK -> LLMGrid"}],
)

print(response.choices[0].message.content)
```

##
