how to use azure open ai gpt-4.1 model ?
# ask-ai
c
how to use azure open ai gpt-4.1 model ?
t
Thanks for reaching out, rajesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thank you for reaching out! Could you please share a bit more about how you’re planning to use the Azure OpenAI GPT-4.1 model? For instance, are you looking for help with setup, integrating it into a specific app, or advice on a particular use case? I’m here to help you every step of the way!
c
here is my code can you siggest me what needs to change ? right now i am using this llm = openai.LLM(model="gpt-4.1", temperature=0.3) what needs to change hetre ?
t
To use the Azure OpenAI GPT-4.1 model, you'll want to set it up with the right Azure parameters. Don't worry, I'll guide you through updating your code step-by-step: First, replace your current line:
Copy code
llm = openai.LLM(model="gpt-4.1", temperature=0.3)
With this configuration:
Copy code
from livekit.plugins.openai import LLM

llm = LLM.with_azure(
    model="gpt-4.1",
    azure_endpoint="https://your-azure-endpoint.openai.azure.com",
    api_key="your-azure-api-key",
    api_version="2024-05-01-preview",
    azure_deployment="your-gpt-4-1-deployment-name"
)
Just a couple of important things to double-check: • Make sure you’ve deployed the GPT-4.1 model in your Azure OpenAI resource. • Confirm that the
azure_endpoint
,
api_key
, and
azure_deployment
values exactly match your Azure setup. • You can also use environment variables like
AZURE_OPENAI_API_KEY
and
AZURE_OPENAI_ENDPOINT
for authentication and endpoint settings, if that’s easier for you. If you have any questions or need a hand with anything else, just let me know—I’m here to help! Sources: plugins/openai/src/llm.ts | examples/voice_agents/realtime_load_chat_history.py | OpenAI LLM models | LiveKit Docs | Build voice, video, and physical AI | LiveKit | Large language models (LLM) | LiveKit Docs
c
can we add max_token here ?
t
Absolutely, you can add a parameter to control the maximum number of tokens in the response. In the LiveKit implementation, this is done using the
max_completion_tokens
parameter. Here’s a little example to help you include it in your configuration:
Copy code
from livekit.plugins.openai import LLM

llm = LLM.with_azure(
    model="gpt-4.1",
    azure_endpoint="https://your-azure-endpoint.openai.azure.com",
    api_key="your-azure-api-key",
    api_version="2024-05-01-preview",
    azure_deployment="your-gpt-4-1-deployment-name",
    max_completion_tokens=500  # Set the maximum number of tokens in the response
)
If you have any questions or need a hand with anything else, just let me know—I’m here to help! Sources: tests/test_config.py | OpenAI LLM models | LiveKit Docs | Build voice, video, and physical AI | LiveKit | DeepSeek LLM | LiveKit Docs
c
llm = LLM.with_azure(\n ^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/llm.py\", line 163, in with_azure\n azure_client = openai.AsyncAzureOpenAI(\n ^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/openai/lib/azure.py\", line 507, in __init__\n base_url = f\"{azure_endpoint.rstrip('/')}/openai/deployments/{azure_deployment}\"\n ^^^^^^^^^^^^^^^^^^^^^\nAttributeError: 'tuple' object has no attribute 'rstrip'", "level": "ERROR", "name": "__mp_main__", "room": "sbx-1rh8j8-BnqkWF4cXPJQLjqZDckZzE", "pid": 92, "job_id": "AJ_kKyJzyBis8p2", "timestamp": "2025-11-01T172309.121311+00:00"} {"message": "unhandled exception while running the job task\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.11/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n return await func(*args, **kwargs) # type: ignore\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 240, in _traceable_entrypoint\n await self._job_entrypoint_fnc(job_ctx)\n File \"/app/livekit_agent.py\", line 514, in entrypoint\n bot = NeuraVoiceAgent(metrics_collector=metrics_collector,\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/livekit_agent.py\", line 231, in __init__\n llm = LLM.with_azure(\n ^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/plugins/openai/llm.py\", line 163, in with_azure\n azure_client = openai.AsyncAzureOpenAI(\n ^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/openai/lib/azure.py\", line 507, in __init__\n base_url = f\"{azure_endpoint.rstrip('/')}/openai/deployments/{azure_deployment}\"\n ^^^^^^^^^^^^^^^^^^^^^\nAttributeError: 'tuple' object has no attribute 'rstrip'", "level": "ERROR", "name": "livekit.agents", "room": "sbx-1rh8j8-BnqkWF4cXPJQLjqZDckZzE", "pid": 92, "job_id": "AJ_kKyJzyBis8p2", "timestamp": "2025-11-01T172309.122592+00:00"} {"message": "process initialized", "level": "INFO", "name": "livekit.agents", "pid": 440, "elapsed_time": 0.75, "timestamp": "2025-11-01T172309.855698+00:00"}
t
It looks like the error is happening because the
azure_endpoint
parameter should be a string, but a tuple was passed instead. This causes the
rstrip
method to fail when building the URL. Let’s work through this together to get it sorted out: 1. First, double-check the value and type of
azure_endpoint
before you pass it to
LLM.with_azure
. You can do this with:
Copy code
import os
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
print(f"Type: {type(endpoint)}, Value: {endpoint}")
1. Make sure that
azure_endpoint
is indeed a properly formatted string URL. If you’re using environment variables, please verify that
AZURE_OPENAI_ENDPOINT
is set correctly and isn’t accidentally being wrapped or parsed as a tuple somewhere in your setup. 2. It’s also a good idea to confirm the type of
azure_endpoint
right before calling
with_azure
like this:
Copy code
if not isinstance(azure_endpoint, str):
    raise TypeError(f"azure_endpoint must be a string, got {type(azure_endpoint)}")
If you run into any other questions or need a hand walking through these steps, please don’t hesitate to reach out—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/__init__.py | Azure OpenAI LLM plugin guide | LiveKit docs
c
"INFO", "name": "livekit.agents", "pid": 439, "timestamp": "2025-11-01T174403.915773+00:00"} {"message": "CRITICAL ERROR in entrypoint: LLM.with_azure() got an unexpected keyword argument 'max_completion_tokens'\nTraceback (most recent call last):\n File \"/app/livekit_agent.py\", line 515, in entrypoint\n bot = NeuraVoiceAgent(metrics_collector=metrics_collector,\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/livekit_agent.py\", line 231, in __init__\n llm = LLM.with_azure(\n ^^^^^^^^^^^^^^^\nTypeError: LLM.with_azure() got an unexpected keyword argument 'max_completion_tokens'", "level": "ERROR", "name": "__mp_main__", "room": "sbx-1rh8j8-YeUnQthY4syQBrg445US2L", "pid": 89, "job_id": "AJ_fQHXoY8x5LK5", "timestamp": "2025-11-01T174403.919034+00:00"} {"message": "unhandled exception while running the job task\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.11/site-packages/opentelemetry/util/_decorator.py\", line 71, in async_wrapper\n return await func(*args, **kwargs) # type: ignore\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.11/site-packages/livekit/agents/ipc/job_proc_lazy_main.py\", line 240, in _traceable_entrypoint\n await self._job_entrypoint_fnc(job_ctx)\n File \"/app/livekit_agent.py\", line 515, in entrypoint\n bot = NeuraVoiceAgent(metrics_collector=metrics_collector,\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/app/livekit_agent.py\", line 231, in __init__\n llm = LLM.with_azure(\n ^^^^^^^^^^^^^^^\nTypeError: LLM.with_azure() got an unexpected keyword argument 'max_completion_tokens'", "level": "ERROR", "name": "livekit.agents", "room": "sbx-1rh8j8-YeUnQthY4syQBrg445US2L", "pid": 89, "job_id": "AJ_fQHXoY8x5LK5", "timestamp": "2025-11-01T174403.919921+00:00"} {"message": "process initialized", "level": "INFO", "name": "livekit.agents", "pid": 439, "elapsed_time": 0.75, "timestamp": "2025-11-01T174404.665511+00:00"}
t
Thanks for reaching out! The error you're seeing happens because the
LLM.with_azure
method doesn’t directly support the
max_completion_tokens
parameter. But don’t worry, you can still include this parameter by setting up the
LLM
class manually instead of using
with_azure
. Here’s a friendly example to guide you:
Copy code
from livekit.plugins.openai import LLM

llm_instance = LLM(
    model="gpt-4.1",
    api_key="your-azure-api-key",
    base_url="https://your-azure-endpoint.openai.azure.com/openai/deployments/your-deployment-name",
    max_completion_tokens=500,  # Set the maximum number of tokens
    temperature=0.7
)
If you’re working with the LiveKit Inference service instead, you can pass
max_completion_tokens
through
extra_kwargs
like this:
Copy code
from livekit.agents.inference import LLM as InferenceLLM

llm_instance = InferenceLLM(
    model="openai/gpt-4.1",
    provider="azureopenai",
    extra_kwargs={"max_completion_tokens": 500}
)
Please feel free to reach out if you’d like me to walk you through a full example tailored to your needs or if you have any other questions. I’m here to help! Sources: livekit-agents/livekit/agents/llm/llm.py | OpenAI LLM models | LiveKit Docs | DeepSeek LLM | LiveKit Docs | Google Gemini LLM | LiveKit Docs
c
i have recived this code from azure import os import base64 from openai import AzureOpenAI from azure.identity import DefaultAzureCredential, get_bearer_token_provider endpoint = os.getenv("ENDPOINT_URL", "https://neuravoice.openai.azure.com/") deployment = os.getenv("DEPLOYMENT_NAME", "gpt-4.1") # Initialize Azure OpenAI client with Entra ID authentication token_provider = get_bearer_token_provider( DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" ) client = AzureOpenAI( azure_endpoint=endpoint, azure_ad_token_provider=token_provider, api_version="2025-01-01-preview", ) # IMAGE_PATH = "YOUR_IMAGE_PATH" # encoded_image = base64.b64encode(open(IMAGE_PATH, 'rb').read()).decode('ascii') chat_prompt = [ { "role": "system", "content": [ { "type": "text", "text": "You are an AI assistant that helps people find information." } ] }, { "role": "user", "content": [ { "type": "text", "text": "Hi" } ] }, { "role": "assistant", "content": [ { "type": "text", "text": "Hello! How can I help you today?" } ] } ] # Include speech result if speech is enabled messages = chat_prompt completion = client.chat.completions.create( model=deployment, messages=messages, max_tokens=13107, temperature=0.7, top_p=0.95, frequency_penalty=0, presence_penalty=0, stop=None, stream=False ) print(completion.to_json()) how to impliment in livekirt ?
t
I'm happy to guide you through implementing the provided Azure OpenAI GPT-4.1 client code in LiveKit! You have two great options to choose from: Method 1: Using the Built-in
withAzure
Method (Recommended)
This is the easiest way, as it automatically sets up the Azure OpenAI client for you:
Copy code
from livekit.plugins.openai import LLM

llm = LLM.with_azure(
    model="gpt-4.1",
    azure_endpoint="https://neuravoice.openai.azure.com",
    api_key="your-azure-api-key",
    api_version="2025-01-01-preview",
    azure_deployment="gpt-4.1"
)
Method 2: Using Your Custom AzureOpenAI Client If you already have your AzureOpenAI client set up and prefer to use it, you can easily pass it to LiveKit's LLM class like this:
Copy code
from livekit.plugins.openai import LLM
from openai import AzureOpenAI

# Your AzureOpenAI client code
client = AzureOpenAI(
    azure_endpoint="https://neuravoice.openai.azure.com",
    azure_ad_token_provider=token_provider,
    api_version="2025-01-01-preview"
)

# Use the client with LiveKit's LLM
llm = LLM(
    model="gpt-4.1",
    client=client,
    temperature=0.7
)
Environment Variables Both methods also support automatic configuration through environment variables, which can make your setup even smoother:
Copy code
AZURE_API_KEY=your-azure-api-key
AZURE_OPENAI_ENDPOINT=https://neuravoice.openai.azure.com
OPENAI_API_VERSION=2025-01-01-preview
If you’d like, I’m here to help with a complete example tailored to your needs or to answer any other questions you might have! Sources: plugins/openai/src/llm.ts | OpenAI LLM models | LiveKit Docs | Build voice, video, and physical AI | LiveKit | OpenAI and LiveKit | LiveKit Docs