From 2529c0641f7e57f77846ebf66c01465d12f2ae47 Mon Sep 17 00:00:00 2001 From: Yasyf Mohamedali Date: Thu, 23 Oct 2025 22:52:05 -0700 Subject: [PATCH] [BAML Adapter] Skip fields that are explicitly excluded from the JSON Schema Excluding a field from the JSON Schema manually normally implies you'd like the LLM to not be aware of it. This is useful in cases where a field is auto-populated and would be confusing to present to the LLM. --- dspy/adapters/baml_adapter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dspy/adapters/baml_adapter.py b/dspy/adapters/baml_adapter.py index 129a6614d7..8262417107 100644 --- a/dspy/adapters/baml_adapter.py +++ b/dspy/adapters/baml_adapter.py @@ -8,6 +8,7 @@ from typing import Any, Literal, Union, get_args, get_origin from pydantic import BaseModel +from pydantic.json_schema import SkipJsonSchema from dspy.adapters.json_adapter import JSONAdapter from dspy.adapters.utils import format_field_value as original_format_field_value @@ -112,6 +113,9 @@ def _build_simplified_schema( lines.append(f"{current_indent}{{") fields = pydantic_model.model_fields + for name, f in fields.copy().items(): + if any(isinstance(a, SkipJsonSchema) for a in f.metadata): + fields.pop(name) if not fields: lines.append(f"{next_indent}{COMMENT_SYMBOL} No fields defined") for name, field in fields.items():