Structured Output (JSON Mode)
To use structured output, simply pass in pydantic model.
Note
Structured Output is not supported with streaming.
from pydantic import BaseModel
from openpo.client import OpenPO
client = OpenPO(api_key="your-huggingface-api-key")
class ResponseModel(BaseModel):
response: str
res = client.chat.completions.create_preference(
model="Qwen/Qwen2.5-Coder-32B-Instruct",
messages=[
{"role": "system", "content": PROMPT},
{"role": "system", "content": MESSAGE},
],
diff_frequency=0.5,
response_format=ResponseModel,
)
For more information on Hugging Face's implementation of structured output, refer here
Warning
OpenRouter does not natively support structured output. This makes the response inconsistent and produces error. Some smaller models on Hugging Face are also prone to inconsistency when structured output is used.