Skip to content

feat(langchain): migrate ChatYandexGPT to Chat API#208

Open
temrjan wants to merge 2 commits intoyandex-cloud:masterfrom
temrjan:feat/langchain-chat-api
Open

feat(langchain): migrate ChatYandexGPT to Chat API#208
temrjan wants to merge 2 commits intoyandex-cloud:masterfrom
temrjan:feat/langchain-chat-api

Conversation

@temrjan
Copy link
Copy Markdown

@temrjan temrjan commented Mar 25, 2026

Summary

Migrates the LangChain integration from the deprecated GRPC Foundation Models API to the modern Chat API (/_chat/completions/), enabling tool call support in ChatYandexGPT.

  • New ChatYandexGPT in _chat/completions/langchain.py using HTTP REST
  • Parse tool_calls from Chat API response into AIMessage.tool_calls
  • Support streaming with tool_call_chunks
  • Add .langchain() method to BaseChatModel
  • Keep langchain as optional dependency
  • Old _models/completions/langchain.py untouched

Changes

File Change
_chat/completions/langchain.py New — ChatYandexGPT adapter for Chat API
_chat/completions/model.py +11 lines.langchain() method
tests/langchain_/test_chat_api.py New — 7 tests
examples/langchain/chat/chat_api_invoke.py New — usage example

Design decisions

  • _transform_messages() converts LangChain messages → Chat API dicts, then existing messages_to_json() handles serialization
  • _parse_tool_calls() filters out malformed tool calls (tc.function is None)
  • Streaming uses tool_call_chunks with args as JSON string (LangChain convention)
  • Sync methods delegate to async via run_sync_impl (same pattern as legacy adapter)
  • Multimodal content supported via list-type HumanMessage.content

Test plan

  • test_ainvoke / test_invoke — basic completion
  • test_astream / test_stream — streaming
  • test_usage_metadata — token usage stats
  • test_response_metadata — finish_reason, model

Next

This PR is the foundation for a follow-up PR adding bind_tools() support (LangChain tools → Yandex FunctionTool conversion + full round-trip).

Refs #142

🤖 Generated with Claude Code



@pytest.mark.asyncio
async def test_ainvoke(model, chat_history):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it is not going to work, because we have no network access at tests.
We usually using pytest-recording for tests like this, recording cassetetes with network responses/requests

>>> result = model.invoke([HumanMessage("Hello!")])
"""

class Config:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered one more thing about our langchain support: we are restricting langchain-core dependency version, because new lanchain have new pydantic dependency, which is breaking backward compatibility.

Probably it have to be fixed if we want to bring this integration back to living world :D

But anyway, this is not about this PR.

class ChatYandexGPT(BaseYandexLanguageModel[ChatAPIModel], LCBaseChatModel):
"""LangChain chat model for Yandex GPT via Chat API.

Uses HTTP ``/_chat/completions/`` (not deprecated GRPC).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm kinda feel that claude didn't understand some contexts here.

I mean, HTTP /_chat/completions/ is means nothing for ordinary user who will read this doc

#!/usr/bin/env python3
"""LangChain invoke example using the Chat API backend.

Uses sdk.chat.completions() instead of sdk.models.completions()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary info for example :)

@temrjan
Copy link
Copy Markdown
Author

temrjan commented Mar 27, 2026

Thanks for the review! Pushed fixes:

Tests — switched to pytest-recording (VCR cassettes), matching the test_completions.py pattern:

  • pytestmark = [pytest.mark.asyncio, pytest.mark.vcr, pytest.mark.require_env('langchain_core')]
  • async_sdk only (no parametrization)
  • 2 focused tests: test_ainvoke (with metadata assertions) + test_astream

Docstrings — removed implementation details (/_chat/completions/, GRPC mentions) from module, class, and example docstrings.

Cassettes — I don't have API access to record them. Could you help record the VCR cassettes, or point me to a test folder_id/API key I could use?

pytest tests/langchain_/test_chat_api.py --record-mode=once --env langchain_core -v

@vhaldemar
Copy link
Copy Markdown
Collaborator

Cassettes — I don't have API access to record them. Could you help record the VCR cassettes, or point me to a test folder_id/API key I could use?

I'm not sure, I will try to ask my collegues, if it is posibble to give you some kind of grant to use yandex cloud.

@vhaldemar
Copy link
Copy Markdown
Collaborator

Hey!

  1. You should get an yandex cloud account with payment method setupped
  2. Create a support ticket, something like "I'm asking for the grant for work with feat(langchain): migrate ChatYandexGPT to Chat API #208"
  3. Post here an ID of support ticket

@temrjan
Copy link
Copy Markdown
Author

temrjan commented Mar 30, 2026

Hey @vhaldemar! Thanks for the guidance.

Done — created a support ticket: KF218173

Let me know if anything else is needed from my side!

@vhaldemar
Copy link
Copy Markdown
Collaborator

Hey, collegues told me your ticket is done.

Also, please? make a rebase or merge to master to fix docs build issue.

Add LangChain integration using the new HTTP Chat API endpoint.
Includes VCR-based tests and example script.
@temrjan temrjan force-pushed the feat/langchain-chat-api branch from 89b9103 to 7a6bd6e Compare March 31, 2026 14:36
Record HTTP cassettes for test_ainvoke and test_astream.
Fix usage_metadata assertion to use dict access.
@temrjan
Copy link
Copy Markdown
Author

temrjan commented Apr 2, 2026

Hey @vhaldemar! Rebased on master, docs build should be fixed now. Cassettes are recorded with real API. Ready for another review when you have time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants