Normalize Tools to Support OpenAI Function Calling Standards #1002
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces built-in normalization for tool definitions within the
ApplyChatTemplate
API in ORT Extensions, enabling support for multiple tool formats, including OpenAI’s function/tool schema.Previously,
ApplyChatTemplate
assumed tools were always passed in the Minja/Phi-4 format, which caused incompatibilities when integrating models or clients that use OpenAI-style tool definitions.With this change, callers can now pass tools in any supported format, and the API will normalize them internally — enabling tool structure–agnostic chat templating. This allows chat templates to render correctly regardless of whether the tools originate from Phi-4, OpenAI, or other ecosystems, without requiring any manual preprocessing on the user’s side.
Additionally, thorough unit tests have been added to validate this behavior across all supported formats.
Key Changes
C++ Tool Normalization
NormalizeTools
function that parses and transforms incoming tool definitions into the Minja/Phi-4 format expected by chat templates.type: "tool"
formattype: "function"
format with nestedfunction
objectParameter Conversion
ConvertParameters
, a helper that detects OpenAI"parameters": { "type": "object", "properties": ... }
schemas and flattens them into a simple parameter mapping.Integration with
ApplyChatTemplate
Extensive Inline Documentation
ConvertParameters
andNormalizeTools
to explain the transformation logic and expected input/output structures.Unit Tests
Phi4ChatTemplate
test to cover all three tool formats, verifying that the rendered chat template output remains correct after normalization.Rationale
Different ecosystems (e.g., OpenAI vs. Phi-4) define tools in subtly different JSON structures. Previously, users had to manually reformat OpenAI tool definitions to match Minja’s expected structure, increasing friction and reducing interoperability.
By introducing a centralized normalization step, this PR ensures:
ApplyChatTemplate
Validation
tool
, OpenAIfunction
)