agent-chat app mode missing app_id in trace metadata (Langfuse/OTLP integrations can't identify the app)
- Dominant language
- TypeScript
- Stars
- 156k
- Forks
- 24.6k
- Avg merge
- 22h 9m
- Merged PRs (30d)
- 610
Description
## Summary
Traces from `agent-chat` apps don't include `app_id` in their metadata, while `advanced-chat` apps do. This breaks third-party tracing integrations (Langfuse, LangSmith, Opik, etc.) that rely on `app_id` to identify which Dify app generated a trace.
## Environment
- Dify version: 1.13.3
- App mode: `agent-chat` (Agent)
- Tracing provider: Langfuse
## Reproduction
1. Create two apps — one `advanced-chat`, one `agent-chat`
2. Enable Langfuse tracing for both (identical config)
3. Send a chat request to each app
4. Inspect the trace metadata in Langfuse
**Expected:** Both traces contain `app_id` in metadata.
**Actual:** Only the `advanced-chat` trace has `app_id`. The `agent-chat` trace is missing it.
## Root cause
In `api/core/ops/ops_trace_manager.py`, the `WorkflowTraceInfo` metadata (used for `advanced-chat`) explicitly includes `app_id`:
```python
metadata = {
...
"app_id": workflow_run.app_id, # ← present
}
```
But the `MessageTraceInfo` metadata (used for `agent-chat`) does not:
```python
metadata = {
"conversation_id": message_data.conversation_id,
"ls_provider": message_data.model_provider,
"ls_model_name": message_data.model_id,
"status": message_data.status,
"from_end_user_id": message_data.from_end_user_id,
"from_account_id": message_data.from_account_id,
"agent_based": message_data.agent_based,
"workflow_run_id": message_data.workflow_run_id,
"from_source": message_data.from_source,
"message_id": message_id,
# ← no app_id
}
```
The code already queries the `Conversation` table for `mode` — the fix extends this to also fetch `app_id`:
```python
# Before:
conversation_mode_stmt = select(Conversation.mode).where(Conversation.id == message_data.conversation_id)
# After:
conversation_mode_stmt = select(Conversation.mode, Conversation.app_id).where(Conversation.id == message_data.conversation_id)
```
And adds `app_id` to the metadata:
```python
metadata = {
"app_id": conversation_app_id, # ← add this
"conversation_id": message_data.conversation_id,
...
}
```
## Impact
Any tracing integration that uses `app_id` from trace metadata to identify the source app will fail for `agent-chat` apps. This affects all trace providers that read `app_id` from `trace_info.metadata` (LangSmith, Opik, Weave, Tencent, Arize Phoenix all have this pattern in their trace implementations).
## Suggested fix
Add `app_id` to the `MessageTraceInfo` metadata by fetching it from the `Conversation` table (which already has an `app_id` column). The conversation is already queried for `mode` — just extend the query.
Contributor guide
Research direction
Start in api/core/ops/ops_trace_manager.py and inspect MessageTraceInfo alongside WorkflowTraceInfo. Extend the existing Conversation lookup to retrieve app_id, include it in agent-chat trace metadata, and verify that agent-chat traces expose the app_id consistently with advanced-chat traces.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, observability-sre
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100