Python: Add Telnyx provider samples (OpenAI-compatible endpoint)#5926
Python: Add Telnyx provider samples (OpenAI-compatible endpoint)#5926gbattistel wants to merge 5 commits into
Conversation
Add samples demonstrating how to use Telnyx as an OpenAI-compatible inference provider with Agent Framework's existing OpenAIChatClient and OpenAIEmbeddingClient. Samples included: - telnyx_chat_completion.py: basic chat with streaming/non-streaming - telnyx_embeddings.py: text embeddings via Telnyx - telnyx_chat_with_tools.py: chat with telecom tools (SMS, lookup) Follows the same pattern as the existing Ollama provider sample. No new provider code — just configuration of base_url and api_key. Refs: microsoft#5925
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds Python samples demonstrating how to use Telnyx’s OpenAI-compatible endpoint (base_url) with the existing Agent Framework OpenAI clients.
Changes:
- Added a Telnyx chat completion sample (streaming + non-streaming) using
OpenAIChatClient - Added a Telnyx embeddings sample using
OpenAIEmbeddingClient - Added a Telnyx provider README with setup and quick start instructions
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| python/samples/02-agents/providers/telnyx/telnyx_chat_completion.py | New Telnyx chat completion sample using a custom base_url |
| python/samples/02-agents/providers/telnyx/telnyx_embeddings.py | New Telnyx embeddings sample using a custom base_url |
| python/samples/02-agents/providers/telnyx/README.md | Documentation for configuring/running the Telnyx samples |
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| import os | ||
|
|
||
| from agent_framework.openai import OpenAIEmbeddingClient | ||
| from dotenv import load_dotenv | ||
|
|
||
| # Load environment variables from .env file | ||
| load_dotenv() | ||
|
|
||
| """ | ||
| Telnyx Embeddings Example | ||
|
|
||
| This sample demonstrates using Telnyx for text embeddings through the | ||
| OpenAIEmbeddingClient by configuring the base_url to point to the | ||
| Telnyx AI API endpoint. | ||
|
|
||
| Telnyx provides an OpenAI-compatible API that supports embeddings | ||
| generation using models like `thenlper/gte-large`. | ||
|
|
||
| Environment Variables: | ||
| TELNYX_API_KEY — Your Telnyx API key (from https://portal.telnyx.com/) | ||
| TELNYX_EMBEDDING_MODEL — Embedding model name (default: "thenlper/gte-large") | ||
| """ |
| # Load environment variables from .env file | ||
| load_dotenv() | ||
|
|
||
| """ | ||
| Telnyx Chat Completion Example | ||
|
|
||
| This sample demonstrates using Telnyx as an OpenAI-compatible inference | ||
| provider through the OpenAIChatClient by configuring the base_url to | ||
| point to the Telnyx AI API endpoint. | ||
|
|
||
| Telnyx provides an OpenAI-compatible API at https://api.telnyx.com/v2/ai/openai | ||
| that supports chat completions, embeddings, and function/tool calling. | ||
|
|
||
| Environment Variables: | ||
| TELNYX_API_KEY — Your Telnyx API key (from https://portal.telnyx.com/) | ||
| TELNYX_MODEL — Model name to use (default: "Kimi-K2.5") | ||
| Available models include: Kimi-K2.5, GLM-5.1-FP8, | ||
| MiniMax-M2.7, Qwen3-235B-A22B | ||
| """ |
| | File | Description | | ||
| |------|-------------| | ||
| | [`telnyx_chat_completion.py`](telnyx_chat_completion.py) | Basic chat completion using `OpenAIChatClient` with Telnyx endpoint. Shows both streaming and non-streaming responses. | | ||
| | [`telnyx_embeddings.py`](telnyx_embeddings.py) | Text embeddings using `OpenAIEmbeddingClient` with Telnyx endpoint. | |
| # Install the Agent Framework OpenAI package | ||
| pip install agent-framework-openai |
| ## Examples | ||
|
|
||
| | File | Description | | ||
| |------|-------------| | ||
| | [`telnyx_chat_completion.py`](telnyx_chat_completion.py) | Basic chat completion using `OpenAIChatClient` with Telnyx endpoint. Shows both streaming and non-streaming responses. | | ||
| | [`telnyx_embeddings.py`](telnyx_embeddings.py) | Text embeddings using `OpenAIEmbeddingClient` with Telnyx endpoint. | |
|
@microsoft-github-policy-service agree company="Telnyx" |
- Move module docstrings before load_dotenv() (top of file) - Fix markdown table syntax in README (remove extra pipes) - Add python-dotenv to Quick Start install instructions - Update PR description to match actual file count (3 files)
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (2)
python/samples/02-agents/providers/telnyx/telnyx_embeddings.py:50
OpenAIEmbeddingClient.get_embeddings()returns a list ofEmbeddingobjects (with.vector/.dimensions), but this loop treats each item as a rawlist[float](len(embedding)and slicingembedding[:5]). This will raise at runtime; useembedding.dimensions(orlen(embedding.vector)) and sliceembedding.vectorinstead.
for i, embedding in enumerate(response):
print(f"Text {i + 1}: \"{texts[i]}\"")
print(f" Dimensions: {len(embedding)}")
print(f" Preview: [{', '.join(str(v) for v in embedding[:5])}, ...]")
python/samples/02-agents/providers/telnyx/telnyx_chat_completion.py:24
- This imports/uses
OpenAIChatClient, which calls the OpenAI Responses API (e.g.,/responses). The PR description says Telnyx provides an OpenAI-compatible endpoint for chat completions, so this sample will likely fail against Telnyx unless Telnyx implements the Responses API. Consider switching toOpenAIChatCompletionClient(Chat Completions API) or clarifying the required endpoint capabilities.
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient
from dotenv import load_dotenv
| | File | Description | | ||
| |------|-------------| | ||
| | `telnyx_chat_completion.py` | Basic chat completion using `OpenAIChatClient` with Telnyx endpoint. Shows both streaming and non-streaming responses. | | ||
| | `telnyx_embeddings.py` | Text embeddings using `OpenAIEmbeddingClient` with Telnyx endpoint. | |
| """Telnyx Chat Completion Example | ||
|
|
||
| This sample demonstrates using Telnyx as an OpenAI-compatible inference | ||
| provider through the OpenAIChatClient by configuring the base_url to | ||
| point to the Telnyx AI API endpoint. |
| """Telnyx Embeddings Example | ||
|
|
||
| This sample demonstrates using Telnyx for text embeddings through the | ||
| OpenAIEmbeddingClient by configuring the base_url to point to the | ||
| Telnyx AI API endpoint. |
The get_embeddings() response returns Embedding objects with .dimensions and .vector attributes, not raw lists. Using len() and slicing directly causes TypeError at runtime.
|
Hi! This PR is ready for review. Both samples have been tested locally and work correctly (chat completion with streaming + non-streaming, and embeddings). The Copilot feedback about the embeddings API usage has been addressed in the latest commit. Would appreciate a review when possible — @eavanvalkenburg @moonbox3 🙏 |
Motivation and Context
Telnyx provides an OpenAI-compatible API endpoint (
https://api.telnyx.com/v2/ai/openai) that supports chat completions and embeddings. Adding provider samples makes it easy for Telnyx users (and anyone using OpenAI-compatible endpoints) to get started with Agent Framework.Related issue: #5925
Description
Adds three files under
python/samples/02-agents/providers/telnyx/:telnyx_chat_completion.py— Chat completion sample usingOpenAIChatClientwith a custombase_url. Demonstrates both streaming and non-streaming responses.telnyx_embeddings.py— Embeddings sample usingOpenAIEmbeddingClientwith a custombase_url.README.md— Setup instructions, environment variables, and quick start guide.This follows the same pattern as the existing Ollama sample (
ollama_with_openai_chat_client.py), where an existing provider client is configured with a custombase_url— no new provider package is needed.Contribution Checklist
OpenAIChatClient/OpenAIEmbeddingClientCloses #5925