DSPy: The framework for programming—not prompting—language models
Python
Expose typed LM API symbols (#9841)
Maxime Rivest4f8803838204
diff --git a/docs/docs/community/normalized-lm-api-migration.md b/docs/docs/community/normalized-lm-api-migration.mdindex 637745d24..bf2b6a1d0 100644--- a/docs/docs/community/normalized-lm-api-migration.md+++ b/docs/docs/community/normalized-lm-api-migration.md@@ -103,8 +103,8 @@ response.provider_dataThe typed path also makes direct `lm(...)` calls more expressive. Strings, typed messages, media parts, previous responses, and explicit `LMRequest` objects all flow through one call API.-!!! warning "Planned 3.3 API"- The examples below illustrate the proposed typed LM call API. Helpers such as `dspy.System`, `dspy.User`, `dspy.Assistant`, `dspy.ToolCall`, and `dspy.ToolResult` are part of the planned implementation and may not exist in the current stable namespace yet.+!!! warning "Experimental 3.3 API"+ The typed LM symbols are importable without `experimental=True`. During the 3.3 release-candidate period, the direct typed `lm(...)` call path will land incrementally behind `dspy.context(experimental=True)` as the API settles. These helpers are expected to be available in DSPy 3.3. Key helpers such as `dspy.LMRequest`, `dspy.LMResponse`, `dspy.System`, `dspy.User`, `dspy.Assistant`, `dspy.ToolCall`, and `dspy.ToolResult` are available at the top level. The complete typed LM vocabulary is available under `dspy.core.types`, e.g. `dspy.core.types.LMTextPart`.Multimodal request with instructions:diff --git a/dspy/__init__.py b/dspy/__init__.pyindex dd23eb7e7..1525ef4a6 100644--- a/dspy/__init__.py+++ b/dspy/__init__.py@@ -7,6 +7,18 @@ from dspy.teleprompt import *from dspy.evaluate import Evaluate # isort: skipfrom dspy.clients import * # isort: skipfrom dspy.adapters import Adapter, ChatAdapter, JSONAdapter, XMLAdapter, TwoStepAdapter, Image, Audio, File, History, Type, Tool, ToolCalls, ToolCallResults, Code, Reasoning # isort: skip+from dspy.core import ( # isort: skip+ Assistant,+ Developer,+ LMConfig,+ LMMessage,+ LMRequest,+ LMResponse,+ System,+ ToolCall,+ ToolResult,+ User,+)from dspy.primitives.sandbox_serializable import SandboxSerializable # isort: skipfrom dspy.utils.exceptions import (AdapterParseError,diff --git a/dspy/core/__init__.py b/dspy/core/__init__.pynew file mode 100644index 000000000..2f72f0024--- /dev/null+++ b/dspy/core/__init__.py@@ -0,0 +1,4 @@+"""Core typed request/response objects used by DSPy internals."""++from dspy.core.types import *+from dspy.core.types import __all__ as __all__diff --git a/dspy/core/types.py b/dspy/core/types.pyindex 6ceb9eb44..5c12a3c4e 100644--- a/dspy/core/types.py+++ b/dspy/core/types.py@@ -14,6 +14,38 @@ from urllib.parse import urlparseimport pydanticfrom pydantic import BaseModel, ConfigDict, Field, model_validator+__all__ = [+ "Assistant",+ "Developer",+ "LMAudioPart",+ "LMBinaryPart",+ "LMCacheConfig",+ "LMCitationPart",+ "LMConfig",+ "LMDocumentPart",+ "LMImagePart",+ "LMMessage",+ "LMOutput",+ "LMPart",+ "LMPromptCacheConfig",+ "LMReasoningConfig",+ "LMRefusalPart",+ "LMRequest",+ "LMResponse",+ "LMTextPart",+ "LMThinkingPart",+ "LMToolCallPart",+ "LMToolChoice",+ "LMToolResultPart",+ "LMToolSpec",+ "LMUsage",+ "LMVideoPart",+ "System",+ "ToolCall",+ "ToolResult",+ "User",+]+class LMBasePart(BaseModel):"""A single content item in an LM message or output."""