Skip to content

fix: llama-stack function tool serialization for Gemini compatibility - #5082

Closed
dprince wants to merge 2 commits into
ogx-ai:mainfrom
dprince:response_streaming_type_fix
Closed

fix: llama-stack function tool serialization for Gemini compatibility#5082
dprince wants to merge 2 commits into
ogx-ai:mainfrom
dprince:response_streaming_type_fix

Conversation

@dprince

@dprince dprince commented Mar 9, 2026

Copy link
Copy Markdown

What does this PR do?

llama-stack's Responses API to Chat Completions conversion in streaming.py leaks a "type" field into the function tool dict via model_dump(). The OpenAI Chat Completions FunctionDefinition schema only allows name, description, parameters, and strict — not type. OpenAI silently ignores the extra field, but Gemini's OpenAI-compatible endpoint strictly validates and rejects it with:
"Unknown name 'type' at 'tools[N].function': Cannot find field."

Fix: exclude type from serialization in streaming.py:
function=input_tool.model_dump(exclude={"type"})

Test Plan

unit tests included

@meta-cla

meta-cla Bot commented Mar 9, 2026

Copy link
Copy Markdown

Hi @dprince!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@dprince dprince changed the title Fix llama-stack function tool serialization for Gemini compatibility fix: llama-stack function tool serialization for Gemini compatibility Mar 9, 2026
@meta-cla

meta-cla Bot commented Mar 9, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 9, 2026
@dprince
dprince force-pushed the response_streaming_type_fix branch from 466ac2f to 12be062 Compare March 10, 2026 10:50

@leseb leseb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave an inline comment explaining why we exclude "type"? Something like:

# Exclude "type" from the function dict — it belongs on the outer
# ChatCompletionToolParam, not inside the FunctionDefinition.
# OpenAI silently ignores the extra field, but stricter endpoints
# (e.g. Gemini) reject it.

This will save future contributors from wondering why the exclude is there.

@dprince
dprince force-pushed the response_streaming_type_fix branch from 26a4834 to ebfb5bf Compare March 10, 2026 17:44
@dprince

dprince commented Mar 10, 2026

Copy link
Copy Markdown
Author

Can you leave an inline comment explaining why we exclude "type"? Something like:

# Exclude "type" from the function dict — it belongs on the outer
# ChatCompletionToolParam, not inside the FunctionDefinition.
# OpenAI silently ignores the extra field, but stricter endpoints
# (e.g. Gemini) reject it.

This will save future contributors from wondering why the exclude is there.

Done, in the latest rebase. Thanks

# (e.g. Gemini) reject it.
self.ctx.chat_tools.append(
ChatCompletionToolParam(type="function", function=input_tool.model_dump(exclude_none=True))
ChatCompletionToolParam(type="function", function=input_tool.model_dump(exclude={"type"}, exclude_none=True))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original code is 1 step too clever using model_dump instead of adding strict to make_openai_tool

also, responses tools have further evolved and allow a defer_loading field, do we need to add that to the exclude too?

two suggestions -

  • directly translate the fields we need and don't use model_dump
  • file an issue about supporting defer_loading, aka tool search

@dprince dprince Mar 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented the switch to make_openai_tool in the latest push

@dprince
dprince force-pushed the response_streaming_type_fix branch 4 times, most recently from 46b3fa6 to de7dab4 Compare March 12, 2026 14:36
@derekhiggins

Copy link
Copy Markdown
Contributor

From https://github.com/llamastack/llama-stack/actions/runs/23007399456/job/66809064669?pr=5082#step:6:2736

         RuntimeError: Recording not found for request hash:                                                            
         5cf1d08147f27ac9aa5bea505ac059a8d0419953c76d84d5611fadce99c8ce68                                               
         Model: gpt-4o | Request: POST https://api.openai.com/v1/v1/chat/completions                                    

It used to be possible to run scripts/github/schedule-record-workflow.sh to generate recordings in CI, but I haven't tried it lately, does anybody know if this is it still possible?

@derekhiggins

Copy link
Copy Markdown
Contributor

@mattf, this is the PR I was talking about (although I see you already here), thanks

@mattf

mattf commented Mar 13, 2026

Copy link
Copy Markdown
Collaborator

this is the PR I was talking about (although I see you already here), thanks

@derekhiggins i've posted gpt recordings and a few ollama, but my current ollama deployment is incomplete

@mergify

mergify Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged. @dprince please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Mar 16, 2026
@dprince
dprince force-pushed the response_streaming_type_fix branch from 30792e4 to ce621d8 Compare March 16, 2026 20:30
@mergify mergify Bot removed the needs-rebase label Mar 16, 2026
@dprince

dprince commented Mar 16, 2026

Copy link
Copy Markdown
Author

@mattf thanks for the recordings. I attempted to rebase those as there were conflicts.

@dprince
dprince force-pushed the response_streaming_type_fix branch from 6eff984 to ce621d8 Compare March 16, 2026 21:00
@dprince
dprince force-pushed the response_streaming_type_fix branch from ce621d8 to aefdc37 Compare March 16, 2026 21:00
@mergify

mergify Bot commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged. @dprince please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Mar 17, 2026
@dprince
dprince force-pushed the response_streaming_type_fix branch from aefdc37 to 4ae9aa8 Compare March 20, 2026 18:15
@mergify mergify Bot removed the needs-rebase label Mar 20, 2026
@mergify

mergify Bot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged. @dprince please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Mar 26, 2026
@nidhishgajjar

This comment was marked as spam.

2 similar comments
@nidhishgajjar

This comment was marked as spam.

@nidhishgajjar

This comment was marked as spam.

@leseb

leseb commented May 7, 2026

Copy link
Copy Markdown
Member

@dprince sorry for the lack of attention here, we have a job to do recordings on behalf of users, can you rebase? thanks

@mergify mergify Bot removed the needs-rebase label May 7, 2026
@mergify

mergify Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged. @dprince please rebase it. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label May 7, 2026
@dprince
dprince force-pushed the response_streaming_type_fix branch from 0c63bfe to be287e7 Compare May 21, 2026 18:53
@mergify mergify Bot removed the needs-rebase label May 21, 2026
@dprince
dprince force-pushed the response_streaming_type_fix branch from be287e7 to d7c272b Compare May 27, 2026 20:46
The Responses API function tool conversion used model_dump() to build
the ChatCompletionToolParam function dict, which leaked the "type"
field (and would leak any future fields like defer_loading) into the
FunctionDefinition. OpenAI silently ignores extra fields, but stricter
endpoints (e.g. Gemini) reject them.

Instead of a fragile denylist (exclude={"type"}), reuse
convert_tooldef_to_openai_tool which explicitly picks only the fields
that belong in a FunctionDefinition. Added strict parameter support to
convert_tooldef_to_openai_tool so function tools can pass it through.

Signed-off-by: Dan Prince <dprince@redhat.com>
@dprince
dprince force-pushed the response_streaming_type_fix branch from d7c272b to d4962f6 Compare June 11, 2026 21:34
@dprince
dprince requested a review from skamenan7 as a code owner June 11, 2026 21:34
Co-Authored-By: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Recordings committed successfully

Recordings from the integration tests have been committed to this PR.

View commit workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants