Skip to content

Commit 4080534

Browse files
committed
Accept video attachments in OpenRouter chats
1 parent 4070c3a commit 4080534

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

lib/ruby_llm/providers/openrouter/chat.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ module Chat
1212

1313
module_function
1414

15+
def format_content(content, attachments = [])
16+
OpenRouter::Media.format_content(content, attachments)
17+
end
18+
1519
def render_payload(messages, tools:, temperature:, model:, stream: false, max_output_tokens: nil, schema: nil,
1620
thinking: nil, citations: false, caching: nil, tool_prefs: nil)
1721
payload = super
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module RubyLLM
4+
module Providers
5+
class OpenRouter
6+
# Handles media content for OpenRouter, which adds video input parts
7+
# to the standard Chat Completions vocabulary.
8+
module Media
9+
module_function
10+
11+
def format_content(content, attachments = [])
12+
Protocols::ChatCompletions::Media.format_parts(content, attachments) do |attachment|
13+
if attachment.type == :video
14+
format_video(attachment)
15+
else
16+
Protocols::ChatCompletions::Media.format_attachment(
17+
attachment, document_attachments: :pdf, image_attachments: true, audio_attachments: true
18+
)
19+
end
20+
end
21+
end
22+
23+
def format_video(video)
24+
{
25+
type: 'video_url',
26+
video_url: {
27+
url: video.url? ? video.source.to_s : video.for_llm
28+
}
29+
}
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
RSpec.describe RubyLLM::Providers::OpenRouter::Media do
5+
include_context 'with configured RubyLLM'
6+
7+
it 'formats video attachments as video_url parts' do
8+
chat = RubyLLM.chat(model: 'openai/gpt-5.2', provider: :openrouter)
9+
chat.add_message(role: :user, content: 'what happens here?', attachments: 'https://example.com/clip.mp4')
10+
11+
part = chat.render[:messages].first[:content].find { |p| p[:type] == 'video_url' }
12+
13+
expect(part).to eq(type: 'video_url', video_url: { url: 'https://example.com/clip.mp4' })
14+
end
15+
end

0 commit comments

Comments
 (0)