Skip to content

fix: respect contextLength from YAML model config#11807

Merged
RomneyDa merged 5 commits intomainfrom
contextLength-respsect
Mar 25, 2026
Merged

fix: respect contextLength from YAML model config#11807
RomneyDa merged 5 commits intomainfrom
contextLength-respsect

Conversation

@RomneyDa
Copy link
Copy Markdown
Contributor

@RomneyDa RomneyDa commented Mar 25, 2026

Summary

Fixes contextLength not being respected when set in YAML config, addressing a long-standing issue reported by multiple users.

  • Add contextLength to YAML model schema at the model level — previously it was only recognized inside defaultCompletionOptions, and zod silently stripped it if placed at the model level (where JSON config supports it)
  • Read contextLength from both locations — model-level takes precedence over defaultCompletionOptions.contextLength
  • Fix GUI selector fallbackselectSelectedChatModelContextLength was falling back to DEFAULT_MAX_TOKENS (4096) instead of DEFAULT_CONTEXT_LENGTH (32768)

Both of these YAML configs now work:

models:
  - name: my-model
    provider: openai
    model: my-model
    contextLength: 65536
    defaultCompletionOptions:
      maxTokens: 32000
models:
  - name: my-model
    provider: openai
    model: my-model
    defaultCompletionOptions:
      contextLength: 65536
      maxTokens: 32000

Closes #4638
Closes #4304

Test plan

  • Set contextLength at model level in YAML config — verify it's respected (no truncation errors)
  • Set contextLength inside defaultCompletionOptions — verify it still works
  • Set contextLength at both levels — verify model-level takes precedence
  • Verify validation warning fires when maxTokens is too close to contextLength at either level

Summary by cubic

Respect contextLength from YAML configs. Model-level contextLength is supported and takes precedence; GUI fallback, validation, and docs now match.

  • Bug Fixes

    • Added contextLength to the YAML model schema at the model level in @continuedev/config-yaml, matching JSON config.
    • Read contextLength from the model level or defaultCompletionOptions, with model-level taking precedence.
    • Fixed GUI selector fallback to use DEFAULT_CONTEXT_LENGTH instead of DEFAULT_MAX_TOKENS.
    • Validation now uses the effective contextLength and maxTokens, warning when they’re too close.
    • Docs: YAML examples use defaultCompletionOptions.contextLength; JSON examples show model-level contextLength. Fixed Ollama guide key name to defaultCompletionOptions.
  • Refactors

    • Replaced find with includes for capability checks in modelConfigToBaseLLM.
    • Extracted env-to-options mapping into applyEnvOptions to reduce complexity.

Written for commit c254c9f. Summary will update on new commits.

Three fixes for contextLength not being respected:

1. Add contextLength to YAML model schema at the model level (not just
   inside defaultCompletionOptions), matching JSON config behavior
2. Read contextLength from both model-level and defaultCompletionOptions,
   with model-level taking precedence
3. Fix GUI selector fallback using DEFAULT_MAX_TOKENS (4096) instead of
   DEFAULT_CONTEXT_LENGTH (32768)

Closes #4638
Closes #4304
@RomneyDa RomneyDa requested a review from a team as a code owner March 25, 2026 04:22
@RomneyDa RomneyDa requested review from Patrick-Erichsen and removed request for a team March 25, 2026 04:22
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 25, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 4 files

@continue
Copy link
Copy Markdown
Contributor

continue bot commented Mar 25, 2026

Test Coverage Review

This PR fixes how contextLength is respected from YAML model config. While the changes are straightforward bug fixes, there's one test gap worth noting:

Suggested Test Addition

core/config/yaml/models.vitest.ts - Consider adding a test case to verify that model.contextLength takes precedence over model.defaultCompletionOptions?.contextLength:

it("should prioritize model.contextLength over defaultCompletionOptions.contextLength", async () => {
  const model: ModelConfig = {
    name: "test-model",
    provider: "openai",
    model: "gpt-4",
    contextLength: 128000, // Should take precedence
    defaultCompletionOptions: {
      contextLength: 64000, // Should be ignored
    },
  };

  const result = await llmsFromModelConfig({
    model,
    uniqueId: "test-id",
    llmLogger: mockLLMLogger,
    config: mockConfig,
  });

  expect(result).toHaveLength(1);
  expect(result[0].contextLength).toBe(128000);
});

it("should fall back to defaultCompletionOptions.contextLength when model.contextLength is not set", async () => {
  const model: ModelConfig = {
    name: "test-model",
    provider: "openai",
    model: "gpt-4",
    defaultCompletionOptions: {
      contextLength: 64000,
    },
  };

  const result = await llmsFromModelConfig({
    model,
    uniqueId: "test-id",
    llmLogger: mockLLMLogger,
    config: mockConfig,
  });

  expect(result).toHaveLength(1);
  expect(result[0].contextLength).toBe(64000);
});

This would ensure the prioritization logic (model.contextLength ?? model.defaultCompletionOptions?.contextLength) is explicitly covered.

No Tests Needed For

  • Schema change (contextLength field addition) - Low risk, validated by zod schema
  • GUI constant fix (DEFAULT_MAX_TOKENSDEFAULT_CONTEXT_LENGTH) - Clear bug fix, correct constant
  • Validation logic - Pre-existing gap (no tests for validateConfigYaml), not introduced by this PR

Replace find callbacks with includes() to offset complexity from
contextLength nullish coalescing.
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 25, 2026
… YAML

YAML examples use defaultCompletionOptions.contextLength, JSON examples
use model-level contextLength. Also fixes ollama guide using the wrong
key name (completionOptions -> defaultCompletionOptions).
Extract the repetitive env-to-options mapping from modelConfigToBaseLLM
into a separate applyEnvOptions function, bringing complexity well under
the limit of 36.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Mar 25, 2026
@github-project-automation github-project-automation bot moved this from Todo to In Progress in Issues and PRs Mar 25, 2026
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 25, 2026
@RomneyDa RomneyDa merged commit d6259a7 into main Mar 25, 2026
69 of 71 checks passed
@RomneyDa RomneyDa deleted the contextLength-respsect branch March 25, 2026 19:26
@github-project-automation github-project-automation bot moved this from In Progress to Done in Issues and PRs Mar 25, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Mar 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

config.yaml contextLength is not applied to model YAML config: contextLength is partly ignored in chat when checking for maxTokens < contextLength

2 participants