tinycodinglm / lmstudio /lmstudio-0120.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
2.46 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How does streaming work with LM Studio's native REST API v1 POST /api/v1/chat? List the SSE event types and show Python SSE handling. Include code examples.

Assistant

Streaming with POST /api/v1/chat - set \"stream\": true for Server-Sent Events (SSE). Each event is event: <name> + data: <json>.

bash
curl -N http://localhost:1234/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '{"model": "model-identifier", "input": "Write a haiku.", "stream": true}'

# event: message_start
# data: {"type":"message_start","output_index":0}
# event: content_delta
# data: {"type":"content_delta","content_index":0,"delta":"Autumn"}
# event: message_end
# data: {"type":"message_end","stats":{}}

Event types:

  • message_start - new output item started
  • content_delta - partial text
  • reasoning_delta - partial reasoning (reasoning models)
  • tool_call_start / tool_call_arguments_delta / tool_call_end - tool calls
  • message_end - generation finished, includes stats
  • model_load_start / model_load_progress / model_load_end - loading status

Python SSE:

python
import json
import requests

resp = requests.post(
    "http://localhost:1234/api/v1/chat",
    json={"model": "model-identifier", "input": "Hello", "stream": True},
    stream=True,
    timeout=60,
)
resp.raise_for_status()
for line in resp.iter_lines(decode_unicode=True):
    if line.startswith("data: "):
        event = json.loads(line[6:])
        if event.get("type") == "content_delta":
            print(event.get("delta", ""), end="", flush=True)
print()

Unlike OpenAI-compat streaming (text deltas only), v1 streaming also emits reasoning, tool-call, and model-load events.