Files
turboderp 79d581e1f5 OAI endpoints: More rework
- remove disconnect_task
- move disconnect logic to a per-request handler that wraps cleanup operation and directly polls the request state with throttling
- exclusively signal disconnect with CancelledError
- rework completions endpoint to follow same approach as chat completions, share some code
- refactor OAI endpoints a bit
- correct behavior for batched completion requests
- make sure logprobs work for completion and streaming completion requests
- more tests
2026-04-02 01:26:44 +02:00

33 lines
949 B
Python

import json
import re
# Markdown code fence patterns
CODE_FENCE_RE = re.compile(r"^```(?:json)?\s*", re.MULTILINE)
CODE_FENCE_END_RE = re.compile(r"\s*```\s*$", re.MULTILINE)
def coerce_param_value(raw: str) -> any:
"""Coerce a raw parameter value string to the appropriate Python type.
Strategy (safe, no eval()):
1. Strip leading/trailing newlines (official template emits \\n
after opening tag and before closing tag).
2. Try json.loads — handles objects, arrays, numbers, bools, null.
3. Fall back to plain string.
"""
# Strip template-inserted newlines around values
stripped = raw.strip()
# Empty string
if not stripped:
return ""
# Try JSON parse (handles objects, arrays, numbers, booleans, null)
try:
return json.loads(stripped)
except (json.JSONDecodeError, ValueError):
pass
# Fall back to string — never eval()
return stripped