Repository navigation
feat: add --extract and --extract-last to output only a fenced code block - #2219
Merged
ksylvan merged 2 commits intoSep 29, 2026
Merged
Conversation
anandghegde
force-pushed
the
feat/extract-code-blocks
branch
from
September 15, 2026 16:38
4b0a61a to
ebfff61
Compare
…lock Print, copy (-c) and write (-o) only the first or last Markdown fenced code block from the model response, like `llm -x/--extract` and `--xl/--extract-last`. If the response has no fenced code block, the full response is used unchanged. The session keeps the full response. With --stream, the streamed content is buffered instead of printed so that only the extracted block is printed at the end.
anandghegde
force-pushed
the
feat/extract-code-blocks
branch
from
September 28, 2026 17:25
ebfff61 to
22660fb
Compare
ksylvan
approved these changes
Sep 29, 2026
ksylvan
left a comment
Collaborator
There was a problem hiding this comment.
Nicely done @anandghegde
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this Pull Request (PR) does
Adds two output flags:
--extract: output only the first Markdown fenced code block in the response--extract-last: output only the last fenced code blockThese work like
llm -x/--extractand--xl/--extract-lastfrom simonw'sllm. They help when a pattern returns code with explanation around it and you only want the code, e.g. to pipe it into a file or a shell.Behaviour:
llm. The help text says this.-cand written with-o, and what goes into the notification. The session still stores the full response, and--output-sessionstill writes the full session.--extract-lastwins, same asllm.or `~~~`, and can have a language tag or other info string. A fence can be 4 or more characters long, which lets a block contain a shorter fence. The closing fence must use the same character and be at least as long as the opening one. A fence can be indented by up to 3 spaces. A backtick "fence" followed by more backticks on the same line (inline ````code``` ````) is not treated as a fence. An unterminated last fence runs to the end of the response, so a response cut off by a token limit still gives you its code. This is the one place where it differs fromllm, which ignores unterminated blocks.--stream: extraction needs the whole response, so the chunks are collected without being printed and only the extracted block is printed at the end. This works the same way--suppress-thinkalready does.llmturns streaming off instead. I kept the vendor request streaming so that--show-metadatausage output and stream error messages still appear. It works through a newChatOptions.BufferStreamfield that the chatter checks before printing each chunk.The extraction itself is the pure function
domain.ExtractFencedCodeBlock(text, last), which has table tests.Help text is registered in
help.go, and translations are added for all 12 locales, following #2179. The new flags are also added to the zsh, bash and fish completions and to the README flag listing. I'll add thecmd/generate_changelog/incoming/<PR>.txtentry once this PR has a number.Related issues
None.
Screenshots
Tested against a local fake OpenAI-compatible server (LM Studio vendor). The model reply had a
```pythonblock and a later~~~bashblock:--extract --output-session -o sess.txtstill wrote the full assistant reply to the file.Testing
go test ./...: everything passes exceptTestValidateImageFileininternal/cli. That test also fails on unmodifiedmainon my machine because/tmp/testalready exists there, so it is unrelated to this change.TestExtractFencedCodeBlock(17 cases),TestChatter_Send_StreamingBufferStreamDoesNotPrint(checked that it fails without the chatter change) andTestBuildChatOptionsExtractBuffersStream.go vet ./...,gofmt -l,goimports -landmodernize ./...all report nothing.Claude Code helped write this change. I reviewed and tested it.
🤖 Generated with Claude Code
https://claude.ai/code/session_01QBWFWqjbUQGS1c9LsM2bxX