import os
import json
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("API_KEY") or "<YOUR API KEY>",
base_url="https://api.siflow.cn/model-api"
)
resp = client.chat.completions.create(
model="MiniMaxAI/MiniMax-M2.7",
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": "What is the capital of France? Please respond as {\"capital\": ...}"}
],
response_format={"type": "json_object"},
temperature=0.3,
max_completion_tokens=256,
stream=False # JSON mode: prefer non-streaming to avoid truncation
)
raw = resp.choices[0].message.content
print("RAW:", raw)
try:
data = json.loads(raw)
print("PARSED:", data)
except json.JSONDecodeError:
# Fallback: log + retry with lower temperature / higher max_completion_tokens / stricter prompt
print("JSON parsing failed; retry with lower temperature or higher max_completion_tokens")