Skip to content

Conversation

christian-bromann
Copy link
Member

Description

This PR fixes an issue where users attempting to use chat/instruct models with the TogetherAI class would encounter a cryptic error: Cannot read properties of undefined (reading 'choices'). This happened because the TogetherAI API returns different response formats for text completion models vs chat models, and users were unknowingly using the wrong class.

Changes Made:

  1. Improved Error Handling: Added proper error handling in the _call method to catch when the API response doesn't match the expected format and provide a helpful error message directing users to use ChatTogetherAI instead.

  2. Model Type Detection: Implemented automatic detection of chat/instruct models (based on patterns like "instruct", "chat", "vision", or "turbo" in model names) and added a console warning when users instantiate TogetherAI with these models.

  3. Documentation Updates:

    • Added clear warnings and a model compatibility table to the TogetherAI documentation
    • Updated ChatTogetherAI documentation to clarify when to use each class
    • Included examples of which models work with which class
  4. Tests: Added unit tests to verify the warning behavior and model type detection.

Example of the fix in action:

Before (confusing error):

const llm = new TogetherAI({
  model: 'meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo',
});
await llm.invoke('Hello'); 
// TypeError: Cannot read properties of undefined (reading 'choices')

After (helpful guidance):

const llm = new TogetherAI({
  model: 'meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo',
});
// Console warning: "Warning: Model 'meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo' appears to be a chat/instruct model. Consider using ChatTogetherAI from @langchain/community/chat_models/togetherai instead."

await llm.invoke('Hello');
// Error: Unexpected response format from Together AI. The model 'meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo' may require the ChatTogetherAI class instead of TogetherAI class.

This slightly improves the developer experience by providing clear guidance on which class to use for different model types.

Fixes #6993

- Add proper error handling when API response format doesn't match expected structure
- Detect chat/instruct models and warn users to use ChatTogetherAI class instead
- Add helpful error message pointing users to the correct class when using incompatible models
- Update documentation to clarify model compatibility between TogetherAI and ChatTogetherAI
- Add unit tests for warning behavior and model type detection

Fixes #6993
Copy link

changeset-bot bot commented Sep 12, 2025

🦋 Changeset detected

Latest commit: c912930

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@langchain/community Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Sep 12, 2025

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

Project Deployment Preview Comments Updated (UTC)
langchainjs-docs Canceled Canceled Comment Sep 12, 2025 9:10pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchainjs-api-refs Ignored Ignored Sep 12, 2025 9:10pm

@hntrl hntrl merged commit e0b48fd into main Sep 13, 2025
35 checks passed
@hntrl hntrl deleted the cb/togetherai-chat-model-confusion-v0.x branch September 13, 2025 01:54
ai-solution-dev pushed a commit to ai-solution-dev/langchainjs that referenced this pull request Sep 16, 2025
hntrl added a commit that referenced this pull request Oct 14, 2025
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.

Togetherai LLM error - Cannot read properties of undefined (reading 'choices')

2 participants