diff --git a/python/packages/core/agent_framework/_skills.py b/python/packages/core/agent_framework/_skills.py index 91bdb61914..80dcafe65e 100644 --- a/python/packages/core/agent_framework/_skills.py +++ b/python/packages/core/agent_framework/_skills.py @@ -675,9 +675,13 @@ def _build_skill_content( ) -> str: """Build XML-structured content for code-defined and class-based skills. - Produces an XML document containing name, description, instructions, - resources, and scripts elements. Used by both :class:`InlineSkill` - and :class:`ClassSkill` to generate their ``content`` property. + Produces an XML document containing name, description, instructions, and + ```` / ```` blocks. The two blocks + are always emitted: when a category has no entries, a self-closing element + (e.g. ````) is emitted so the model knows none are + available and does not hallucinate their names. Used by both + :class:`InlineSkill` and :class:`ClassSkill` to generate their ``content`` + property. Args: name: The skill name. @@ -698,13 +702,8 @@ def _build_skill_content( "" ) - if resources: - resource_lines = "\n".join(_create_resource_element(r) for r in resources) - result += f"\n\n\n{resource_lines}\n" - - if scripts: - script_lines = "\n".join(_create_script_element(s) for s in scripts) - result += f"\n\n\n{script_lines}\n" + result += f"\n\n{_build_available_resources_block(resources)}" + result += f"\n\n{_build_available_scripts_block(scripts)}" return result @@ -725,6 +724,50 @@ def _create_resource_element(resource: SkillResource) -> str: return f" " +def _build_available_resources_block(resources: Sequence[SkillResource] | None) -> str: + """Build an ```` XML block for the given resources. + + Each resource is emitted as a ```` element (with an + optional ``description`` attribute). When there are no resources, a + self-closing ```` element is returned so the model + knows none are available and does not hallucinate resource names. + + Args: + resources: The resources to include in the block, if any. + + Returns: + The ```` XML block, or ```` + when *resources* is empty or ``None``. + """ + if not resources: + return "" + resource_lines = "\n".join(_create_resource_element(r) for r in resources) + return f"\n{resource_lines}\n" + + +def _build_available_scripts_block(scripts: Sequence[SkillScript] | None) -> str: + """Build an ```` XML block for the given scripts. + + Each script is emitted as a ``