Skip to content

Conversation

ishahroz
Copy link
Contributor

@ishahroz ishahroz commented Aug 27, 2025

Description: This PR adds support for DeepSeek's beta strict mode feature for structured
outputs and tool calling. It overrides bind_tools() and with_structured_output() to automatically use
DeepSeek's beta endpoint (https://api.deepseek.com/beta) when strict=True. Both methods need overriding because they're independent entry points and user can call either directly. When DeepSeek's strict mode graduates from beta, we can just remove both overriden methods. You can read more about the beta feature here: https://api-docs.deepseek.com/guides/function_calling#strict-mode-beta

Issue: Implements #32670

Dependencies: None

Sample Code

from langchain_deepseek import ChatDeepSeek
from pydantic import BaseModel, Field
from typing import Optional
import os


# Enter your DeepSeek API Key here
API_KEY = "YOUR_API_KEY"


# location, temperature, condition are required fields
# humidity is optional field with default value
class WeatherInfo(BaseModel):
    location: str = Field(description="City name")
    temperature: int = Field(description="Temperature in Celsius")
    condition: str = Field(description="Weather condition (sunny, cloudy, rainy)")
    humidity: Optional[int] = Field(default=None, description="Humidity percentage")


llm = ChatDeepSeek(
    model="deepseek-chat",
    api_key=API_KEY,
)

# just to confirm that a new instance will use the default base url (instead of beta)
print(f"Default API base: {llm.api_base}")



# Test 1: bind_tools with strict=True shoud list all the tools calls
print("\nTest 1: bind_tools with strict=True")
llm_with_tools = llm.bind_tools([WeatherInfo], strict=True)
response = llm_with_tools.invoke("Tell me the weather in New York. It's 22 degrees, sunny.")
print(response.tool_calls)



# Test 2: with_structured_output with strict=True
print("\nTest 2: with_structured_output with strict=True")
structured_llm = llm.with_structured_output(WeatherInfo, strict=True)
result = structured_llm.invoke("Tell me the weather in New York.")
print(f"  Result: {result}")
assert isinstance(result, WeatherInfo), "Result should be a WeatherInfo instance"

Copy link

vercel bot commented Aug 27, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain Ignored Ignored Preview Aug 27, 2025 11:39pm

Copy link

codspeed-hq bot commented Aug 27, 2025

CodSpeed WallTime Performance Report

Merging #32727 will not alter performance

Comparing ishahroz:feat/deepseek-strict-mode-beta-support (5831da0) with master (7f9b077)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched benchmarks

@ishahroz ishahroz changed the title feat: add strict mode (beta) support for deepseek chat model feat(deepseek): strict Mode Beta Support Aug 27, 2025
@ishahroz ishahroz changed the title feat(deepseek): strict Mode Beta Support feat(deepseek): beta Support strict Mode Aug 27, 2025
Copy link

codspeed-hq bot commented Aug 27, 2025

CodSpeed Instrumentation Performance Report

Merging #32727 will not alter performance

Comparing ishahroz:feat/deepseek-strict-mode-beta-support (5831da0) with master (7f9b077)

Summary

✅ 14 untouched benchmarks

@ishahroz ishahroz marked this pull request as ready for review August 27, 2025 23:40
@mdrxy mdrxy added the integration Related to a provider partner package integration label Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration Related to a provider partner package integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants