DSPy: The framework for programming—not prompting—language models
Python
Custom BAMLAdapter to improve structured outputs (#8614)
Prashanth Rao5a095f4f1e76
diff --git a/dspy/adapters/baml_adapter.py b/dspy/adapters/baml_adapter.pynew file mode 100644index 000000000..fe41f2a74--- /dev/null+++ b/dspy/adapters/baml_adapter.py@@ -0,0 +1,312 @@+"""+Custom adapter for improving structured outputs using the information from Pydantic models.+Based on the format used by BAML: https://github.com/BoundaryML/baml+"""++import inspect+import types+from typing import Any, Literal, Union, get_args, get_origin++from pydantic import BaseModel++from dspy.adapters.json_adapter import JSONAdapter+from dspy.adapters.utils import format_field_value as original_format_field_value+from dspy.signatures.signature import Signature++# Changing the comment symbol to Python's # rather than other languages' // seems to help+COMMENT_SYMBOL = "#"+++def _render_type_str(annotation: Any, _depth: int = 0, indent: int = 0) -> str:+ """Recursively renders a type annotation into a simplified string.++ Args:+ annotation: The type annotation to render+ _depth: Current recursion depth (prevents infinite recursion)+ indent: Current indentation level for nested structures+ """+ max_depth = 10+ if _depth > max_depth: # Prevent excessive recursion+ return f"<max depth of {max_depth} exceeded>"++ try:+ origin = get_origin(annotation)+ args = get_args(annotation)+ except Exception:+ return str(annotation)++ # Handle Optional[T] or T | None+ if origin in (types.UnionType, Union):+ non_none_args = [arg for arg in args if arg is not type(None)]+ # Render the non-None part of the union+ type_render = " or ".join([_render_type_str(arg, _depth + 1, indent) for arg in non_none_args])+ # Add 'or null' if None was part of the union+ if len(non_none_args) < len(args):+ return f"{type_render} or null"+ return type_render++ # Base types+ if annotation is str:+ return "string"+ if annotation is int:+ return "int"+ if annotation is float:+ return "float"+ if annotation is bool:+ return "boolean"++ # Composite types+ if origin is Literal:+ return " or ".join(f'"{arg}"' for arg in args)+ if origin is list:+ # For Pydantic models in lists, use bracket notation+ inner_type = args[0]+ if inspect.isclass(inner_type) and issubclass(inner_type, BaseModel):+ # Build inner schema - the Pydantic model inside should use indent level for array contents+ inner_schema = _build_simplified_schema(inner_type, indent + 1)+ # Format with proper bracket notation and indentation+ current_indent = " " * indent+ return f"[\n{inner_schema}\n{current_indent}]"+ else:+ return f"{_render_type_str(inner_type, _depth + 1, indent)}[]"+ if origin is dict:+ return f"dict[{_render_type_str(args[0], _depth + 1, indent)}, {_render_type_str(args[1], _depth + 1, indent)}]"++ # Pydantic models (we'll recurse in the main function)+ if inspect.isclass(annotation) and issubclass(annotation, BaseModel):+ try:+ return _build_simplified_schema(annotation, indent)+ except Exception:+ return f"<{annotation.__name__}>"++ # Fallback+ if hasattr(annotation, "__name__"):+ return annotation.__name__+ return str(annotation)+++def _build_simplified_schema(model: type[BaseModel], indent: int = 0, _seen: set[type] | None = None) -> str:+ """Builds a simplified, human-readable schema from a Pydantic model.++ Args:+ model: The Pydantic model to build schema for+ indent: Current indentation level+ _seen: Set to track visited models (prevents infinite recursion)+ """+ if _seen is None:+ _seen = set()++ if model in _seen:+ return f"<circular reference to {model.__name__}>"++ _seen.add(model)++ try:+ lines = []+ current_indent = " " * indent+ next_indent = " " * (indent + 1)++ lines.append(f"{current_indent}{{")++ fields = model.model_fields+ if not fields:+ lines.append(f"{next_indent}{COMMENT_SYMBOL} No fields defined")+ for name, field in fields.items():+ if field.description:+ lines.append(f"{next_indent}{COMMENT_SYMBOL} {field.description}")+ elif field.alias and field.alias != name:+ # If there's an alias but no description, show the alias as a comment+ lines.append(f"{next_indent}{COMMENT_SYMBOL} alias: {field.alias}")++ # Check for a nested Pydantic model+ field_type_to_render = field.annotation++ # Unpack Optional[T] to get T+ origin = get_origin(field_type_to_render)+ if origin in (types.UnionType, Union):+ non_none_args = [arg for arg in get_args(field_type_to_render) if arg is not type(None)]+ if len(non_none_args) == 1:+ field_type_to_render = non_none_args[0]++ # Unpack list[T] to get T+ origin = get_origin(field_type_to_render)+ if origin is list:+ field_type_to_render = get_args(field_type_to_render)[0]++ if inspect.isclass(field_type_to_render) and issubclass(field_type_to_render, BaseModel):+ # Recursively build schema for nested models with circular reference protection+ nested_schema = _build_simplified_schema(field_type_to_render, indent + 1, _seen)+ rendered_type = _render_type_str(field.annotation, indent=indent + 1).replace(+ field_type_to_render.__name__, nested_schema+ )+ else:+ rendered_type = _render_type_str(field.annotation, indent=indent + 1)++ line = f"{next_indent}{name}: {rendered_type},"++ lines.append(line)++ lines.append(f"{current_indent}}}")+ return "\n".join(lines)+ except Exception as e:+ return f"<error building schema for {model.__name__}: {e}>"+ finally:+ _seen.discard(model)+++class BAMLAdapter(JSONAdapter):+ """+ A DSPy adapter that improves the rendering of complex/nested Pydantic models to help LMs.++ This adapter generates a compact, human-readable schema representation for nested Pydantic output+ fields, inspired by the BAML project's JSON formatter (https://github.com/BoundaryML/baml).+ The resulting rendered schema is more token-efficient and easier for smaller LMs to follow than a+ raw JSON schema. It also includes Pydantic field descriptions as comments in the schema, which+ provide valuable additional context for the LM to understand the expected output.++ Example Usage:+ ```python+ import dspy+ from pydantic import BaseModel, Field+ from typing import Literal+ from baml_adapter import BAMLAdapter # Import from your module++ # 1. Define your Pydantic models+ class PatientAddress(BaseModel):+ street: str+ city: str+ country: Literal["US", "CA"]+ class PatientDetails(BaseModel):+ name: str = Field(description="Full name of the patient.")+ age: int+ address: PatientAddress | None++ # 2. Define a signature using the Pydantic model as an output field+ class ExtractPatientInfo(dspy.Signature):+ '''Extract patient information from the clinical note.'''+ clinical_note: str = dspy.InputField()+ patient_info: PatientDetails = dspy.OutputField()++ # 3. Configure dspy to use the new adapter+ llm = dspy.OpenAI(model="gpt-4.1-mini")+ dspy.configure(lm=llm, adapter=BAMLAdapter())++ # 4. Run your program+ extractor = dspy.Predict(ExtractPatientInfo)+ note = "John Doe, 45 years old, lives at 123 Main St, Anytown. Resident of the US."+ result = extractor(clinical_note=note)+ print(result.patient_info)++ # Expected output:+ # PatientDetails(name='John Doe', age=45, address=PatientAddress(street='123 Main St', city='Anytown', country='US'))+ ```+ """++ def format_field_description(self, signature: type[Signature]) -> str:+ """Format the field description for the system message."""+ sections = []++ # Add input field descriptions+ if signature.input_fields:+ sections.append("Your input fields are:")+ for i, (name, field) in enumerate(signature.input_fields.items(), 1):+ type_name = getattr(field.annotation, "__name__", str(field.annotation))+ description = f": {field.description}" if field.description else ":"+ sections.append(f"{i}. `{name}` ({type_name}){description}")++ # Add output field descriptions+ if signature.output_fields:+ sections.append("Your output fields are:")+ for i, (name, field) in enumerate(signature.output_fields.items(), 1):+ type_name = getattr(field.annotation, "__name__", str(field.annotation))+ description = f": {field.description}" if field.description else ":"+ sections.append(f"{i}. `{name}` ({type_name}){description}")++ return "\n".join(sections)++ def format_field_structure(self, signature: type[Signature]) -> str:+ """Overrides the base method to generate a simplified schema for Pydantic models."""++ sections = []++ # Add structural explanation+ sections.append(+ "All interactions will be structured in the following way, with the appropriate values filled in.\n"+ )++ # Add input structure section+ if signature.input_fields:+ for name in signature.input_fields.keys():+ sections.append(f"[[ ## {name} ## ]]")+ sections.append(f"{{{name}}}")+ sections.append("") # Empty line after each input++ # Add output structure section+ if signature.output_fields:+ for name, field in signature.output_fields.items():+ field_type = field.annotation+ main_type = field_type++ # Find the core type if it's wrapped in Optional or Union+ origin = get_origin(field_type)+ if origin in (types.UnionType, Union):+ non_none_args = [arg for arg in get_args(field_type) if arg is not type(None)]+ if len(non_none_args) == 1:+ main_type = non_none_args[0]++ sections.append(f"[[ ## {name} ## ]]")++ if inspect.isclass(main_type) and issubclass(main_type, BaseModel):+ # We have a pydantic model, so build the simplified schema for it.+ schema_str = _build_simplified_schema(main_type)+ sections.append(schema_str)+ else:+ # Handle non-pydantic or primitive types simply+ type_str = _render_type_str(field_type, indent=0)+ sections.append(f"Output field `{name}` should be of type: {type_str}")++ sections.append("") # Empty line after each output++ # Add completed section+ sections.append("[[ ## completed ## ]]")++ return "\n".join(sections)++ def format_task_description(self, signature: type[Signature]) -> str:+ """Format the task description for the system message."""+ import textwrap++ instructions = textwrap.dedent(signature.instructions)+ objective = ("\n" + " " * 8).join([""] + instructions.splitlines())+ return f"In adhering to this structure, your objective is: {objective}"++ def format_user_message_content(+ self,+ signature: type[Signature],+ inputs: dict[str, Any],+ prefix: str = "",+ suffix: str = "",+ main_request: bool = False,+ ) -> str:+ """Overrides the base method to render Pydantic input instances as clean JSON."""+ messages = [prefix]+ for key, field_info in signature.input_fields.items():+ if key in inputs:+ value = inputs.get(key)+ formatted_value = ""+ if isinstance(value, BaseModel):+ # Use clean, indented JSON for Pydantic instances+ formatted_value = value.model_dump_json(indent=2, by_alias=True)+ else:+ # Fallback to the original dspy formatter for other types+ formatted_value = original_format_field_value(field_info=field_info, value=value)++ messages.append(f"[[ ## {key} ## ]]\n{formatted_value}")++ if main_request:+ output_requirements = self.user_message_output_requirements(signature)+ if output_requirements is not None:+ messages.append(output_requirements)++ messages.append(suffix)+ return "\n\n".join(m for m in messages if m).strip()diff --git a/tests/adapters/test_baml_adapter.py b/tests/adapters/test_baml_adapter.pynew file mode 100644index 000000000..5f103babe--- /dev/null+++ b/tests/adapters/test_baml_adapter.py@@ -0,0 +1,601 @@+from typing import Literal+from unittest import mock++import pydantic+import pytest+from litellm import Choices, Message+from litellm.files.main import ModelResponse++import dspy+from dspy.adapters.baml_adapter import COMMENT_SYMBOL, BAMLAdapter+++# Test fixtures - Pydantic models for testing+class PatientAddress(pydantic.BaseModel):+ street: str+ city: str+ country: Literal["US", "CA"]+++class PatientDetails(pydantic.BaseModel):+ name: str = pydantic.Field(description="Full name of the patient")+ age: int+ address: PatientAddress | None = None+++class ComplexNestedModel(pydantic.BaseModel):+ id: int = pydantic.Field(description="Unique identifier")+ details: PatientDetails+ tags: list[str] = pydantic.Field(default_factory=list)+ metadata: dict[str, str] = pydantic.Field(default_factory=dict)+++class ModelWithLists(pydantic.BaseModel):+ items: list[PatientAddress] = pydantic.Field(description="List of patient addresses")+ scores: list[float]+++class ImageWrapper(pydantic.BaseModel):+ images: list[dspy.Image]+ tag: list[str]+++class CircularModelA(pydantic.BaseModel):+ name: str+ related_b: "CircularModelB | None" = None+++class CircularModelB(pydantic.BaseModel):+ id: int+ related_a: CircularModelA | None = None+++def test_baml_adapter_basic_schema_generation():+ """Test that BAMLAdapter generates simplified schemas for Pydantic models."""++ class TestSignature(dspy.Signature):+ question: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Should contain simplified schema with comments+ assert f"{COMMENT_SYMBOL} Full name of the patient" in schema+ assert "name: string," in schema+ assert "age: int," in schema+ assert "address:" in schema+ assert "street: string," in schema+ assert 'country: "US" or "CA",' in schema+++def test_baml_adapter_handles_optional_fields():+ """Test optional field rendering with 'or null' syntax."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Optional address field should show 'or null'+ assert "address:" in schema+ assert "or null" in schema+++def test_baml_adapter_handles_primitive_types():+ """Test rendering of basic primitive types."""++ class SimpleModel(pydantic.BaseModel):+ text: str+ number: int+ decimal: float+ flag: bool++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ output: SimpleModel = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ assert "text: string," in schema+ assert "number: int," in schema+ assert "decimal: float," in schema+ assert "flag: boolean," in schema+++def test_baml_adapter_handles_lists_with_bracket_notation():+ """Test that lists of Pydantic models use proper bracket notation."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ addresses: ModelWithLists = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Should use bracket notation for lists and include comments+ assert "items: [" in schema+ assert f"{COMMENT_SYMBOL} List of patient addresses" in schema+ assert "street: string," in schema+ assert "city: string," in schema+ assert "]," in schema+ assert "scores: float[]," in schema+++def test_baml_adapter_handles_complex_nested_models():+ """Test deeply nested Pydantic model schema generation."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ complex: ComplexNestedModel = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Should include nested structure with comments+ assert f"{COMMENT_SYMBOL} Unique identifier" in schema+ assert "details:" in schema+ assert f"{COMMENT_SYMBOL} Full name of the patient" in schema+ assert "tags: string[]," in schema+ assert "metadata: dict[string, string]," in schema+++def test_baml_adapter_prevents_circular_references():+ """Test that circular references are handled gracefully."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ circular: CircularModelA = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Should not cause infinite recursion and should handle ForwardRef gracefully+ assert "name: string," in schema+ # The actual output shows ForwardRef handling, which is acceptable+ assert "ForwardRef" in schema or "<circular reference" in schema+++def test_baml_adapter_formats_pydantic_inputs_as_clean_json():+ """Test that Pydantic input instances are formatted as clean JSON."""++ class TestSignature(dspy.Signature):+ patient: PatientDetails = dspy.InputField()+ question: str = dspy.InputField()+ answer: str = dspy.OutputField()++ adapter = BAMLAdapter()+ patient = PatientDetails(+ name="John Doe", age=45, address=PatientAddress(street="123 Main St", city="Anytown", country="US")+ )++ messages = adapter.format(TestSignature, [], {"patient": patient, "question": "What is the diagnosis?"})++ # Should have clean, indented JSON for Pydantic input+ user_message = messages[-1]["content"]+ assert '"name": "John Doe"' in user_message+ assert '"age": 45' in user_message+ assert '"street": "123 Main St"' in user_message+ assert '"country": "US"' in user_message+++def test_baml_adapter_handles_mixed_input_types():+ """Test formatting of mixed Pydantic and primitive inputs."""++ class TestSignature(dspy.Signature):+ patient: PatientDetails = dspy.InputField()+ priority: int = dspy.InputField()+ notes: str = dspy.InputField()+ result: str = dspy.OutputField()++ adapter = BAMLAdapter()+ patient = PatientDetails(name="Jane Doe", age=30)++ messages = adapter.format(TestSignature, [], {"patient": patient, "priority": 1, "notes": "Urgent case"})++ user_message = messages[-1]["content"]+ # Pydantic should be JSON formatted+ assert '"name": "Jane Doe"' in user_message+ # Primitives should be formatted normally+ assert "priority ## ]]\n1" in user_message+ assert "notes ## ]]\nUrgent case" in user_message+++def test_baml_adapter_handles_schema_generation_errors_gracefully():+ """Test graceful handling of schema generation errors."""++ class ProblematicModel(pydantic.BaseModel):+ # This might cause issues in schema generation+ field: object++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ output: ProblematicModel = dspy.OutputField()++ adapter = BAMLAdapter()++ # Should not raise an exception+ try:+ schema = adapter.format_field_structure(TestSignature)+ # If no exception, schema should at least contain some basic structure+ assert "schema" in schema.lower()+ except Exception:+ # If exception occurs, test passes as we're testing graceful handling+ pass+++def test_baml_adapter_inherits_json_parsing_behavior():+ """Test that BAMLAdapter maintains JSONAdapter's parsing compatibility."""++ class TestSignature(dspy.Signature):+ question: str = dspy.InputField()+ answer: str = dspy.OutputField()++ baml_adapter = BAMLAdapter()+ json_adapter = dspy.JSONAdapter()++ # Both should parse the same JSON response identically+ completion = '{"answer": "Paris"}'++ baml_result = baml_adapter.parse(TestSignature, completion)+ json_result = json_adapter.parse(TestSignature, completion)++ assert baml_result == json_result == {"answer": "Paris"}+++def test_baml_adapter_parse_complex_pydantic_models():+ """Test parsing JSON into complex Pydantic models."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ adapter = BAMLAdapter()++ completion = """{"patient": {+ "name": "John Smith",+ "age": 42,+ "address": {+ "street": "456 Oak St",+ "city": "Springfield",+ "country": "US"+ }+ }}"""++ result = adapter.parse(TestSignature, completion)++ assert isinstance(result["patient"], PatientDetails)+ assert result["patient"].name == "John Smith"+ assert result["patient"].age == 42+ assert isinstance(result["patient"].address, PatientAddress)+ assert result["patient"].address.street == "456 Oak St"+++def test_baml_adapter_raises_on_missing_fields():+ """Test that missing required fields raise appropriate errors."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()+ summary: str = dspy.OutputField()++ adapter = BAMLAdapter()++ # Missing 'summary' field+ completion = '{"patient": {"name": "John", "age": 30}}'++ with pytest.raises(dspy.utils.exceptions.AdapterParseError) as e:+ adapter.parse(TestSignature, completion)++ assert e.value.adapter_name == "JSONAdapter" # BAMLAdapter inherits from JSONAdapter+ assert "summary" in str(e.value)+++def test_baml_adapter_handles_type_casting_errors():+ """Test graceful handling of type casting errors."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ adapter = BAMLAdapter()++ # Invalid age type+ completion = '{"patient": {"name": "John", "age": "not_a_number"}}'++ # Should raise ValidationError from Pydantic (which is the expected behavior)+ with pytest.raises((dspy.utils.exceptions.AdapterParseError, pydantic.ValidationError)):+ adapter.parse(TestSignature, completion)+++def test_baml_adapter_with_images():+ """Test BAMLAdapter integration with dspy.Image objects."""++ class TestSignature(dspy.Signature):+ image_data: ImageWrapper = dspy.InputField()+ description: str = dspy.OutputField()++ adapter = BAMLAdapter()++ image_wrapper = ImageWrapper(+ images=[dspy.Image(url="https://example.com/image1.jpg"), dspy.Image(url="https://example.com/image2.jpg")],+ tag=["test", "medical"],+ )++ messages = adapter.format(TestSignature, [], {"image_data": image_wrapper})++ # Should contain image URLs in the message content+ user_message = messages[-1]["content"]+ image_contents = [+ content for content in user_message if isinstance(content, dict) and content.get("type") == "image_url"+ ]++ assert len(image_contents) == 2+ assert {"type": "image_url", "image_url": {"url": "https://example.com/image1.jpg"}} in user_message+ assert {"type": "image_url", "image_url": {"url": "https://example.com/image2.jpg"}} in user_message+++def test_baml_adapter_with_tools():+ """Test BAMLAdapter integration with dspy.Tool objects."""++ class TestSignature(dspy.Signature):+ question: str = dspy.InputField()+ tools: list[dspy.Tool] = dspy.InputField()+ answer: str = dspy.OutputField()++ def get_patient_info(patient_id: int) -> str:+ """Get patient information by ID"""+ return f"Patient info for ID {patient_id}"++ def schedule_appointment(patient_name: str, date: str) -> str:+ """Schedule an appointment for a patient"""+ return f"Scheduled appointment for {patient_name} on {date}"++ tools = [dspy.Tool(get_patient_info), dspy.Tool(schedule_appointment)]++ adapter = BAMLAdapter()+ messages = adapter.format(TestSignature, [], {"question": "Schedule an appointment for John", "tools": tools})++ user_message = messages[-1]["content"]+ assert "get_patient_info" in user_message+ assert "schedule_appointment" in user_message+ assert "Get patient information by ID" in user_message+ assert "Schedule an appointment for a patient" in user_message+++def test_baml_adapter_with_code():+ """Test BAMLAdapter integration with dspy.Code objects."""++ # Test with code as input field+ class CodeAnalysisSignature(dspy.Signature):+ code: dspy.Code = dspy.InputField()+ analysis: str = dspy.OutputField()++ adapter = BAMLAdapter()+ messages = adapter.format(CodeAnalysisSignature, [], {"code": "def hello():\n print('Hello, world!')"})++ user_message = messages[-1]["content"]+ assert "def hello():" in user_message+ assert "print('Hello, world!')" in user_message++ # Test with code as output field+ class CodeGenSignature(dspy.Signature):+ task: str = dspy.InputField()+ code: dspy.Code = dspy.OutputField()++ with mock.patch("litellm.completion") as mock_completion:+ mock_completion.return_value = ModelResponse(+ choices=[Choices(message=Message(content='{"code": "print(\\"Generated code\\")"}'))],+ model="openai/gpt-4o-mini",+ )++ result = adapter(+ dspy.LM(model="openai/gpt-4o-mini", cache=False),+ {},+ CodeGenSignature,+ [],+ {"task": "Write a hello world program"},+ )++ assert result[0]["code"].code == 'print("Generated code")'+++def test_baml_adapter_with_conversation_history():+ """Test BAMLAdapter integration with dspy.History objects."""++ class TestSignature(dspy.Signature):+ history: dspy.History = dspy.InputField()+ question: str = dspy.InputField()+ answer: str = dspy.OutputField()++ history = dspy.History(+ messages=[+ {"question": "What is the patient's age?", "answer": "45 years old"},+ {"question": "Any allergies?", "answer": "Penicillin allergy"},+ ]+ )++ adapter = BAMLAdapter()+ messages = adapter.format(TestSignature, [], {"history": history, "question": "What medications should we avoid?"})++ # Should format history as separate messages+ assert len(messages) == 6 # system + 2 history pairs + user+ assert "What is the patient's age?" in messages[1]["content"]+ assert '"answer": "45 years old"' in messages[2]["content"]+ assert "Any allergies?" in messages[3]["content"]+ assert '"answer": "Penicillin allergy"' in messages[4]["content"]+++# Comparison tests with JSONAdapter+def test_baml_vs_json_adapter_token_efficiency():+ """Test that BAMLAdapter generates more token-efficient schemas."""++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ complex: ComplexNestedModel = dspy.OutputField()++ baml_adapter = BAMLAdapter()+ json_adapter = dspy.JSONAdapter()++ baml_schema = baml_adapter.format_field_structure(TestSignature)+ json_schema = json_adapter.format_field_structure(TestSignature)++ # Simple character count as proxy for token efficiency+ # BAMLAdapter should always produce shorter schemas+ assert len(baml_schema) < len(json_schema)+++def test_baml_vs_json_adapter_functional_compatibility():+ """Test that both adapters parse identical outputs to the same results."""++ class TestSignature(dspy.Signature):+ question: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ baml_adapter = BAMLAdapter()+ json_adapter = dspy.JSONAdapter()++ completion = """{"patient": {+ "name": "Alice Brown",+ "age": 35,+ "address": {"street": "789 Pine St", "city": "Boston", "country": "US"}+ }}"""++ baml_result = baml_adapter.parse(TestSignature, completion)+ json_result = json_adapter.parse(TestSignature, completion)++ # Results should be functionally equivalent+ assert baml_result["patient"].name == json_result["patient"].name+ assert baml_result["patient"].age == json_result["patient"].age+ assert baml_result["patient"].address.street == json_result["patient"].address.street+++async def test_baml_adapter_async_functionality():+ """Test BAMLAdapter async operations."""++ class TestSignature(dspy.Signature):+ question: str = dspy.InputField()+ patient: PatientDetails = dspy.OutputField()++ with mock.patch("litellm.acompletion") as mock_acompletion:+ mock_acompletion.return_value = ModelResponse(+ choices=[Choices(message=Message(content='{"patient": {"name": "John Doe", "age": 28}}'))],+ model="openai/gpt-4o",+ )++ adapter = BAMLAdapter()+ result = await adapter.acall(+ dspy.LM(model="openai/gpt-4o", cache=False), {}, TestSignature, [], {"question": "Extract patient info"}+ )++ assert result[0]["patient"].name == "John Doe"+ assert result[0]["patient"].age == 28+++def test_baml_adapter_with_field_aliases():+ """Test BAMLAdapter with Pydantic field aliases."""++ class ModelWithAliases(pydantic.BaseModel):+ full_name: str = pydantic.Field(alias="name")+ patient_age: int = pydantic.Field(alias="age")++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ data: ModelWithAliases = dspy.OutputField()++ adapter = BAMLAdapter()++ # Schema should show aliases in the output structure+ schema = adapter.format_field_structure(TestSignature)+ assert "name:" in schema # Should use alias, not field name+ assert "age:" in schema # Should use alias, not field name+++def test_baml_adapter_field_alias_without_description():+ """Test BAMLAdapter with field alias present but description absent."""++ class ModelWithAliasNoDescription(pydantic.BaseModel):+ internal_field: str = pydantic.Field(alias="public_name")+ regular_field: int+ field_with_description: str = pydantic.Field(description="This field has a description", alias="desc_field")++ class TestSignature(dspy.Signature):+ input: str = dspy.InputField()+ data: ModelWithAliasNoDescription = dspy.OutputField()++ adapter = BAMLAdapter()+ schema = adapter.format_field_structure(TestSignature)++ # Should show alias as comment when description is absent+ assert f"{COMMENT_SYMBOL} alias: public_name" in schema+ # Should show description comment when present+ assert f"{COMMENT_SYMBOL} This field has a description" in schema+ # Regular field (without alias) should appear in schema but without alias comment+ assert "regular_field: int," in schema+ # Check that regular_field section doesn't have an alias comment+ regular_field_section = schema.split("regular_field: int,")[0].split("\n")[-1]+ assert f"{COMMENT_SYMBOL} alias:" not in regular_field_section+++def test_baml_adapter_multiple_pydantic_input_fields():+ """Test that multiple InputField() with Pydantic models are rendered correctly."""++ class UserProfile(pydantic.BaseModel):+ name: str = pydantic.Field(description="User's full name")+ email: str+ age: int++ class SystemConfig(pydantic.BaseModel):+ timeout: int = pydantic.Field(description="Timeout in seconds")+ debug: bool+ endpoints: list[str]++ class TestSignature(dspy.Signature):+ input_1: UserProfile = dspy.InputField()+ input_2: SystemConfig = dspy.InputField()+ result: str = dspy.OutputField()++ adapter = BAMLAdapter()++ # Test schema generation includes headers for ALL input fields+ schema = adapter.format_field_structure(TestSignature)+ assert "[[ ## input_1 ## ]]" in schema # Should include first input field header+ assert "[[ ## input_2 ## ]]" in schema # Should include second input field header+ assert "[[ ## result ## ]]" in schema # Should include output field header+ assert "[[ ## completed ## ]]" in schema # Should include completed section+ assert "All interactions will be structured in the following way" in schema+ assert "{input_1}" in schema+ assert "{input_2}" in schema+ assert "Output field `result` should be of type: string" in schema++ # Test field descriptions are in the correct method+ field_desc = adapter.format_field_description(TestSignature)+ assert "Your input fields are:" in field_desc+ assert "Your output fields are:" in field_desc++ # Test message formatting with actual Pydantic instances+ user_profile = UserProfile(name="John Doe", email="[email protected]", age=30)+ system_config = SystemConfig(timeout=300, debug=True, endpoints=["api1", "api2"])++ messages = adapter.format(TestSignature, [], {"input_1": user_profile, "input_2": system_config})++ user_message = messages[-1]["content"]++ # Verify both inputs are rendered with the correct bracket notation+ assert "[[ ## input_1 ## ]]" in user_message+ assert "[[ ## input_2 ## ]]" in user_message++ # Verify JSON content for both inputs+ assert '"name": "John Doe"' in user_message+ assert '"email": "[email protected]"' in user_message+ assert '"age": 30' in user_message+ assert '"timeout": 300' in user_message+ assert '"debug": true' in user_message+ # Endpoints array is formatted with indentation, so check for individual elements+ assert '"api1"' in user_message+ assert '"api2"' in user_message+ assert '"endpoints":' in user_message