> ## Documentation Index
> Fetch the complete documentation index at: https://arizeai-433a7140.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Qwen Tracing

> Trace Qwen model calls in Phoenix through Alibaba Cloud Model Studio's OpenAI-compatible API with OpenInference auto-instrumentation for the OpenAI Python SDK.

[Qwen](https://qwenlm.github.io/) models are available through Alibaba Cloud Model Studio, which provides an OpenAI-compatible API mode. Configure the OpenAI Python client with your Model Studio endpoint and use [`openinference-instrumentation-openai`](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai) to capture spans in Phoenix.

> [!NOTE]
>
> This page instruments OpenAI SDK calls to Alibaba Cloud Model Studio's OpenAI-compatible endpoint. It is not a separate Qwen-specific OpenInference instrumentor.

## Install

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
pip install arize-phoenix-otel openinference-instrumentation-openai openai
```

## Setup

Add your DashScope or Model Studio API key and the OpenAI-compatible base URL for your region as environment variables:

```bash theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
export DASHSCOPE_API_KEY="<your-dashscope-api-key>"
export QWEN_BASE_URL="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
```

> [!TIP]
>
> Alibaba Cloud may provide a workspace-specific or regional compatible-mode endpoint. Replace `QWEN_BASE_URL` with the endpoint shown in your Model Studio console if it differs.

Connect your application to Phoenix with the `register` function:

from phoenix.otel import register

&#x20; \# configure the Phoenix tracer
&#x20; tracer\_provider = register(
&#x20; project\_name="my-llm-app", # Default is 'default'
&#x20; auto\_instrument=True # Auto-instrument your app based on installed OI dependencies
&#x20; )

## Run Qwen

```python theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import os
from openai import OpenAI

client = OpenAI(
    base_url=os.environ["QWEN_BASE_URL"],
    api_key=os.environ["DASHSCOPE_API_KEY"],
)

response = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {
            "role": "user",
            "content": "Why should AI agent traces include tool-call context?",
        },
    ],
)

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

## Observe

Now that you have tracing set up, all invocations of the OpenAI client pointed at Qwen will be streamed to your running Phoenix for observability and evaluation. Each call appears as a `ChatCompletion` LLM span with the prompt, response, model name, token usage, and latency.

## Common Issues

* **Spans missing**: `register(..., auto_instrument=True)` must run before your application sends requests, and `openinference-instrumentation-openai` must be installed in the same environment
* **`401` from Model Studio**: Use your DashScope or Model Studio API key, not an OpenAI key
* **Endpoint or model errors**: Confirm `QWEN_BASE_URL` matches your Model Studio region or workspace, and replace `qwen-plus` with an available Qwen model

## Resources

> **[Model Studio OpenAI Compatibility](https://www.alibabacloud.com/help/en/model-studio/compatibility-of-openai-with-dashscope)**
>
> Alibaba Cloud Model Studio docs

> **[OpenInference OpenAI Instrumentation](https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai)**
>
> OpenAI instrumentation package
