Agent Support
How to build AI agents using structured outputs such as JSON and function/tool calls with CentML
Both Serverless and Dedicated LLM APIs from CentML support structured outputs using the same OpenAI-compatible API. These features are crucial for agentic workloads that require reliable data parsing and function calling. The CentML platform also provides reasoning-enabled models (e.g., DeepSeek-AI/deepseek-r1
) that can perform reasoning before generating structured outputs.
JSON Schema Output
When you need a response strictly formatted as JSON, you can use JSON schema constraints. This is particularly useful in scenarios where your system or other downstream processes rely on valid JSON.
Below is an example of how to set up a request to the CentML LLM API using JSON schema enforcement.
How it Works
- Prompt Construction: Provide a system message telling the model to respond in JSON, along with the JSON schema itself.
- Schema Enforcement: In the
response_format
parameter, specify"type": "json_schema"
and include your JSON schema definition. - Parsing the Output: The response is guaranteed to follow the schema (as strictly as the model can enforce it). You can then parse it directly in your application.
Tool (Function) Calling
CentML’s LLM APIs support function calling similarly to OpenAI’s “function calling” feature. This allows you to define “tools” that the model can call with structured parameters. For example, you might have a get_weather
function your model can invoke based on user requests.
How it Works
- Tool Definition: In the
tools
parameter, define a function with aname
,description
, and a JSON schema for parameters. - Function Invocation: The model may decide to call the function (tool), returning the parameters it deems relevant based on user input.
- Your Application Logic: You receive the structured function arguments in
completion.choices[0].message.tool_calls
. You can then handle them programmatically (e.g., call an actual weather service).
Tip: Make sure your model (e.g., gpt-4o
or deepseek-ai/DeepSeek-R1
) is function-call capable. Not all model versions support function calling. Always check the CentML model documentation for compatibility.
Best Practices and Tips
- Schema Validation: The model will try to adhere to your schema, but always perform server-side validation before using the data (especially important in production).
- Temperature Setting: When generating structured data, lower the temperature to reduce the likelihood of extraneous or incorrect fields.
- Use Reasoning Models: For complex tasks requiring reasoning steps, consider using
deepseek-ai/DeepSeek-R1
. It provides chain-of-thought style reasoning capabilities. - Production Hardening: If you’re building an agentic or workflow-driven application, ensure you handle potential parsing errors and fallback scenarios gracefully.
Both Serverless and Dedicated LLM APIs share the same interface and usage pattern. You only need to change the base URL (and any relevant credentials) if you switch between them.
Conclusion
Using JSON schema enforcement and function calling (tools) with CentML LLM APIs lets you build more reliable, agentic, and automated workflows. With minimal changes to your existing OpenAI-compatible code, you can take advantage of these features on the CentML platform—whether you’re deploying on Serverless or Dedicated.
For more details, visit our CentML Documentation or reach out on Slack if you have any questions!