Skip to content

[BUG] property defaults to type string even if type is defined via anyOf #1101

@wistuba

Description

@wistuba

Checks

  • I have updated to the latest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

latest

Python Version

3.12

Operating System

AL2

Installation Method

pip

Steps to Reproduce

import copy

from strands.tools import normalize_tool_spec


def test_schema():
    schema = {
      "name": "sleep",
      "description": "Pause execution for the specified number of seconds.\n\nThis tool pauses the execution flow for the given number of seconds.\nIt can be interrupted with SIGINT (Ctrl+C).\n\nArgs:\n    seconds (Union[int, float]): Number of seconds to sleep.\n        Must be a positive number greater than 0 and less than or equal to\n        the maximum allowed value (default: 300 seconds, configurable via\n        MAX_SLEEP_SECONDS environment variable).\n\nReturns:\n    str: A message indicating the sleep completed or was interrupted.\n\nRaises:\n    ValueError: If seconds is not positive, exceeds the maximum allowed value,\n               or is not a number.\n\nExamples:\n    >>> sleep(5)  # Sleeps for 5 seconds\n    'Started sleep at 2025-05-30 11:30:00, slept for 5.0 seconds'\n\n    >>> sleep(0.5)  # Sleeps for half a second\n    'Started sleep at 2025-05-30 11:30:00, slept for 0.5 seconds'",
      "inputSchema": {
        "json": {
          "properties": {
            "seconds": {
              "anyOf": [
                {
                  "type": "integer"
                },
                {
                  "type": "number"
                }
              ],
              "description": "Number of seconds to sleep.\nMust be a positive number greater than 0 and less than or equal to\nthe maximum allowed value (default: 300 seconds, configurable via\nMAX_SLEEP_SECONDS environment variable)."
            }
          },
          "required": [
            "seconds"
          ],
          "type": "object"
        }
      }
    }
    assert normalize_tool_spec(copy.deepcopy(schema)) == schema

Expected Behavior

don't add default type "string" in case there is "anyOf". maybe never add it.

Actual Behavior

There is a bug in _normalize_property, in particular:

normalized_prop.setdefault("type", "string")

pytest output:

                        'equal to\n'
                        'the maximum allowed value (default: 300 seconds, '
                        'configurable via\n'
                        'MAX_SLEEP_SECONDS environment variable).',
+                       'type': 'string',
                    },
                },
                'required': [
                    'seconds',
                ],
                'type': 'object',
            },
        },
        'name': 'sleep',
    }

Additional Context

No response

Possible Solution

No response

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions