instructure/canvas-lms · error · ContentTooLarge

Content exceeds # character limit

Error message

Content exceeds #{MAX_CONTENT_CHARS} character limit

What it means

StudyAssist::ContentTooLarge is raised by resolve_content when the resolved content's extracted text exceeds MAX_CONTENT_CHARS characters. The service caps prompt size to protect LLM token limits and latency; oversized content is rejected rather than truncated.

Solutions

  1. Use shorter content (split the page or select a portion before passing page_id/file_id)
  2. Chunk the content and make multiple StudyAssist calls on smaller excerpts
  3. Raise MAX_CONTENT_CHARS if the LLM context window genuinely allows it
  4. Verify extraction isn't inflating text (e.g. duplicated hidden content) via html_to_text

Example fix

// before
StudyAssist.new(course:, user:, prompt:, page_id: 'huge-page').call
// after
page_text = html_to_text(page.body)
StudyAssist.new(course:, user:, prompt:, page_id: 'huge-page').call if page_text.length <= StudyAssist::MAX_CONTENT_CHARS
Defensive patterns

Strategy: validation

Validate before calling

content_text = resolved_text(course:, page_id:, file_id:)
raise StudyAssist::ContentTooLarge if content_text.length > StudyAssist::MAX_CONTENT_CHARS

Type guard

null

Try / catch

begin
  StudyAssist.new(course:, user:, prompt:, page_id:).call
rescue StudyAssist::ContentTooLarge
  render json: { error: 'content too large; select a shorter excerpt' }, status: :unprocessable_entity
end

Prevention

When it happens

Trigger: Calling #call with page_id or file_id whose extracted text length > MAX_CONTENT_CHARS (e.g. a very long wiki page or large text file), after successful content resolution.

Common situations: Extremely long course pages; concatenated/merged documents; uploads of big text files that pass the byte check on file size but extract into more characters than allowed; MAX_CONTENT_CHARS lowered in a config change making previously working content too large.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/ef2178bbf43c895e. Report an issue: GitHub.

Appendix: source

Thrown at app/services/study_assist.rb:150

    end

    # --- Content resolution ---

    def resolve_content
      page_id = @state["pageID"] || @state[:pageID]
      file_id = @state["fileID"] || @state[:fileID]

      content =
        if page_id.present?
          resolve_page(page_id)
        elsif file_id.present?
          resolve_file(file_id)
        else
          raise ContentUnavailable, "No pageID or fileID provided"
        end

      if content.text.length > MAX_CONTENT_CHARS
        raise ContentTooLarge, "Content exceeds #{MAX_CONTENT_CHARS} character limit"
      end

      content
    end

    def resolve_page(page_id)
      page = @course.wiki_pages.not_deleted.find_by(url: page_id)
      raise ContentUnavailable, "Page not found" if page.nil?
      raise ContentUnavailable, "Page access denied" unless page.grants_right?(@user, :read)

      shard_safe_key = shard_safe_cache_key_for(page)
      text = Rails.cache.fetch(text_cache_key_for(:page, shard_safe_key), expires_in: TEXT_CACHE_TTL) do
        html_to_text(page.body.to_s)
      end

      Content.new(kind: :page, id: page.id, cache_key_with_version: shard_safe_key, text:)
    end

View on GitHub (pinned to 1c9f0bb801)