Skip to content

Conversation

@rvguha
Copy link
Collaborator

@rvguha rvguha commented Oct 27, 2025

Summary

Implement ChatGPT App specification v0.5 output format to enable rich UI widgets in ChatGPT Apps. This allows NLWeb to display search results using ChatGPT's native list widgets.

Backend Changes

Core Files Modified

  • core/baseHandler.py: Add output_format parameter (default: "default")
  • core/schemas.py: Add format_response_to_chatgpt_spec() utility function
  • core/utils/message_senders.py: Transform responses and filter messages for chatgptapp format
  • webserver/mcp_wrapper.py: Add output_format to MCP ask tool (defaults to "chatgptapp")

Response Format

When output_format=chatgptapp:

SSE Stream:

data: {"_meta": {"conversation_id": "", "version": "0.5", "openai/outputTemplate": "ui://widget/list.html", "query_rewrite": {...}, "decontextualized_query": "..."}}
data: {"content": [{"type": "resource", "resource": {"data": [{"@type": "Item", ...}]}}]}
data: {"content": [{"type": "resource", "resource": {"data": [...]}}]}

Filtered Messages:

  • Skip: begin-nlweb-response, end-nlweb-response, query_rewrite, decontextualized_query, and all other non-result messages
  • Only _meta (sent once) and content blocks are included

Frontend Changes

  • static/index.html: Set SSE as default connection type (was WebSocket)
  • Users can still use WebSocket with ?mode=websocket

Key Features

  • ✅ Full backward compatibility - default format unchanged
  • ✅ Preserves all data fields from original format
  • ✅ Enables ChatGPT Apps SDK generic list widget
  • ✅ Works with any schema.org type (Restaurant, Recipe, Article, Product, etc.)
  • ✅ Clean output with only _meta and content blocks
  • ✅ MCP defaults to chatgptapp format

Usage

HTTP/SSE:

/ask?query=pizza&site=seriouseats&output_format=chatgptapp

MCP:

{
  "name": "ask",
  "arguments": {
    "query": "pizza",
    "site": ["seriouseats"],
    "output_format": "chatgptapp"
  }
}

Test Plan

  • Tested with production endpoint
  • Verified all fields preserved
  • Confirmed only _meta and content messages sent
  • Validated ChatGPT App spec v0.5 compliance
  • Tested MCP integration

rvguha and others added 6 commits October 27, 2025 06:08
Implement ChatGPT App specification v0.5 output format to enable rich UI widgets in ChatGPT. This allows NLWeb to display search results using ChatGPT's native list widgets.

Backend changes:
- Add output_format parameter to NLWebHandler (default: "default")
- Transform responses to ChatGPT App format when output_format=chatgptapp
- Send _meta block once with conversation_id, version, openai/outputTemplate, query_rewrite, and decontextualized_query
- Stream content blocks as {"type": "resource", "resource": {"data": [...]}}
- Filter out non-essential messages (begin/end-nlweb-response, query_rewrite, decontextualized_query, etc.)
- Only _meta and content messages are sent for chatgptapp format
- MCP defaults to chatgptapp format

Frontend changes:
- Set SSE as default connection type (was WebSocket)
- Users can still use WebSocket with ?mode=websocket

Key features:
- Full backward compatibility - default format unchanged
- Preserves all data fields from original format
- Enables ChatGPT Apps SDK generic list widget (ui://widget/list.html)
- Works with any schema.org type (Restaurant, Recipe, Article, Product, etc.)
- Clean output with only _meta and content blocks
Change field names in _meta to use nlweb/ prefix for NLWeb-specific fields:
- conversation_id -> nlweb/conversationId
- version -> nlweb/version
- query_rewrite -> nlweb/queryRewrite
- decontextualized_query -> nlweb/decontextualizedQuery

This follows the ChatGPT App SDK convention of namespacing custom fields
to avoid conflicts with OpenAI fields like openai/outputTemplate.
- Skip nlweb/conversationId if empty
- Skip nlweb/decontextualizedQuery if same as original query
- Remove nlweb/queryRewrite field entirely (not needed)

This keeps the _meta block minimal with only relevant fields.
For output_format=chatgptapp, transform each result item:
- Flatten all schema_object fields to top level
- Replace schema_object with grounding field containing the URL
- Preserve all existing top-level fields

Example: {schema_object: {headline: ...}} → {grounding: url, headline: ...}

This provides richer structured data for ChatGPT Apps widgets.
When transforming for chatgptapp format:
- If top-level @type is 'Item' and schema_object has @type, replace with schema @type
- Preserves specific schema.org types (Article, Recipe, etc.) instead of generic Item
- Non-Item @type values are preserved as-is

Example: {@type: 'Item', schema_object: {@type: ['Article']}} → {@type: ['Article']}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants