Skip to content

Instantly share code, notes, and snippets.

@intellectronica
Last active December 26, 2024 19:08
Show Gist options
  • Save intellectronica/9b190aca94bf4372c4b08e8b016922ec to your computer and use it in GitHub Desktop.
Save intellectronica/9b190aca94bf4372c4b08e8b016922ec to your computer and use it in GitHub Desktop.
pydantic-ai-openai-strict.ipynb
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@samuelcolvin
Copy link

Nice! You can probably simplify _map_tool_definition to

def _map_tool_definition(f: ToolDefinition) -> ChatCompletionToolParam:
    tool_param = super()._map_tool_definition(f)
    tool_param['function']['strict'] = True
    return tool_param

@intellectronica
Copy link
Author

Nice! You can probably simplify _map_tool_definition to

def _map_tool_definition(f: ToolDefinition) -> ChatCompletionToolParam:
    tool_param = super()._map_tool_definition(f)
    tool_param['function']['strict'] = True
    return tool_param

@samuelcolvin Almost. super() can't be used like this, but we can just call the method from the class explicitly. And we need to also set additionalProperties.

This works:

  @staticmethod
  def _map_tool_definition(f: ToolDefinition) -> ChatCompletionToolParam:
      tool_def = OpenAIModel._map_tool_definition(f)
      tool_def['function']['strict'] = True
      tool_def['function']['parameters']['additionalProperties'] = False
      return tool_def

@intellectronica
Copy link
Author

Gist updated. Thanks for the suggestion, this is more elegant and less duplicative.

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