Skip to content

tools - do not remove $defs #294

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 1 commit into from
Jun 26, 2025
Merged

tools - do not remove $defs #294

merged 1 commit into from
Jun 26, 2025

Conversation

pgrayy
Copy link
Member

@pgrayy pgrayy commented Jun 26, 2025

Description

The strands tool decorator is removing "$defs" from the generated tool spec, which leads to validation errors or poor quality responses. Take for example the following tool:

class TimeSpec(pydantic.BaseModel):
    """City name and timezone."""

    city: str = pydantic.Field(description="City name, e.g., New York.")
    timezone: str = pydantic.Field(description="Time zone, e.g., EST.")


@strands.tool
def current_time(spec: TimeSpec) -> str:
    return ...

The resulting tool spec is:

{
  "name": "current_time",
  "description": "current_time",
  "input_schema": {
    "properties": {
      "spec": {
        "$ref": "#/$defs/TimeSpec",
        "description": "Parameter spec"
      }
    },
    "required": [
      "spec"
    ],
    "type": "object"
  }
}

Notice however that the "$ref" remains. I tested this on a few model providers. If I run through bedrock, I encounter the following error:

botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the ConverseStream operation: The model returned the following errors: tools.0.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12 (https://json-schema.org/draft/2020-12). Learn more about tool use at https://docs.anthropic.com/en/docs/tool-use.

If I run this through openai, I do not receive a validation error but instead a poor quality response:

I'm sorry, I can't provide the current time right now. However, you can quickly find the current time in New York by doing a quick web search or by checking the world clock function of your device.

This PR removes the filtering of "$defs". My example tool schema then looks as follows:

{
  "name": "current_time",
  "description": "current_time",
  "input_schema": {
    "$defs": {
      "TimeSpec": {
        "description": "City name and timezone.",
        "properties": {
          "city": {
            "description": "City name, e.g., New York.",
            "title": "City",
            "type": "string"
          },
          "timezone": {
            "description": "Time zone, e.g., EST.",
            "title": "Timezone",
            "type": "string"
          }
        },
        "required": [
          "city",
          "timezone"
        ],
        "title": "TimeSpec",
        "type": "object"
      }
    },
    "properties": {
      "spec": {
        "$ref": "#/$defs/TimeSpec",
        "description": "Parameter spec"
      }
    },
    "required": [
      "spec"
    ],
    "type": "object"
  }
}

Related Issues

#247

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

  • I ran hatch run prepare
  • I also directly invoked each model provider (minus LlamaAPIModel) with my sample tool included in the spec. All successfully called the tool.
    • I don't have a LlamaAPIModel key which is why this one was excluded. However, I did test the llama3.3 model through the OllamaModel provider.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pgrayy pgrayy requested review from awsarron and dbschmigelski June 26, 2025 12:46
@pgrayy pgrayy merged commit 38c4f78 into strands-agents:main Jun 26, 2025
32 checks passed
@pgrayy pgrayy deleted the defs-bug branch June 27, 2025 13:16
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.

2 participants