Skip to content

types: allow items and defs for tools #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025

Conversation

ParthSareen
Copy link
Member

@ParthSareen ParthSareen commented Apr 8, 2025

@sheffler
Copy link

sheffler commented Apr 9, 2025

Glad to see you working on the python client. FYI In my testing, to get it to emit the field named "$defs" (it has an alias) in file _client.py I needed to change the model_dump() to include by_alias=True

return self._request(
      ChatResponse,
      'POST',
      '/api/chat',
      json=ChatRequest(
        model=model,
        messages=[message for message in _copy_messages(messages)],
        tools=[tool for tool in _copy_tools(tools)],
        stream=stream,
        format=format,
        options=options,
        keep_alive=keep_alive,
      ).model_dump(exclude_none=True, by_alias=True),
      stream=stream,
    )

@ParthSareen
Copy link
Member Author

ParthSareen commented Apr 9, 2025

Glad to see you working on the python client. FYI In my testing, to get it to emit the field named "$defs" (it has an alias) in file _client.py I needed to change the model_dump() to include by_alias=True

I'll give that a look - didn't test thoroughly yet but initial test looked fine. Thanks for the heads up!

@ParthSareen ParthSareen force-pushed the parth/allow-items-and-defs-tools branch from 8bd689f to 8a52e8e Compare April 11, 2025 18:05
@sheffler
Copy link

Hi Parth -
I've been watching this and trying out your change. It didn't pass through "$defs" for me. However, I followed your lead and found a way to keep the changes local instead of requiring a change to the top level.

However, it requires Pydantic >=2.11.0, and that may not be acceptable to the project.

Here is the code that worked for me.

class Tool(SubscriptableBaseModel):
  type: Optional[Literal['function']] = 'function'

  class Function(SubscriptableBaseModel):
    name: Optional[str] = None
    description: Optional[str] = None

    class Parameters(SubscriptableBaseModel):
      model_config = ConfigDict(serialize_by_alias=True)

      type: Optional[Literal['object']] = 'object'
      required: Optional[Sequence[str]] = None
      defs: Optional[Any] = Field(None, alias="$defs")

      class Property(SubscriptableBaseModel):
        model_config = ConfigDict(arbitrary_types_allowed=True)

        type: Optional[str] = None
        description: Optional[str] = None
        items: Optional[Dict[str, Any]] = None

      properties: Optional[Mapping[str, Property]] = None

    parameters: Optional[Parameters] = None

  function: Optional[Function] = None

With this change, "oterm" was able to pass through the complex json schema with the $defs clause.

@ParthSareen
Copy link
Member Author

@sheffler trying something similar to what we do already want to give this branch another shot?

@sheffler
Copy link

will try it out tomorrow.

@sheffler
Copy link

sheffler commented Apr 15, 2025

Done! I performed a test using oterm with an MCP Tool that has a $defs clause in the JSON Schema. It worked and the $defs clause was passed through correctly.

😃

@ParthSareen
Copy link
Member Author

Awesome! thanks for the double check @sheffler!

@ParthSareen ParthSareen requested a review from mxyng April 15, 2025 17:55
@model_serializer(mode='wrap')
def serialize_model(self, nxt):
output = nxt(self)
if 'tools' in output and output['tools']:
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the same pattern as the create request

@ParthSareen ParthSareen merged commit 65f94b4 into main Apr 16, 2025
7 checks passed
@ParthSareen ParthSareen deleted the parth/allow-items-and-defs-tools branch April 16, 2025 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants