Several weeks ago I came across interesting 13th century comments recorded in the Shiltei Giborim [on the Rif] to Avodah Zarah 20. These comments address the question of whether midrashim are intended to be taken literally. You can find a text of the Hebrew - corrected from manuscripts - on-line here, along with some very important footnotes.
Here's a quick translation of the Shiltei Giborim's words:
Know and understand that there are three paths in midrash:
1) Some [midrashim] exaggerate, as Chullin 90b says, "The Torah spoke words of emptiness, the prophets spoke words of emptiness, the sages spoke words of emptiness," such as in Deuteronomy 1:28 "great, fortified cities in the heavens," and Kings I 1:40 "the earth split due to their voice."
There are many of these, like the words of Rabbah bar bar Chanah in Bava Batra 73b; these were exaggeration, for people speak thus.
2) Some of the midrashim present miracles, in which Gd demonstrates His might and displays amazing and shocking deeds, as in Daniel 10:7, "And I, Daniel alone, saw the vision, and the people with me did not see, etc." And Yonah ben Amitai who was swallowed by the fish and spat out. And many others like this.
Many of these are found in the words of the sages, such as Bava Batra 58a regarding R' Bena'ah marking caves, and Bava Batra 58a with a magician digging in the caves of the dead. All of those were miracles, as were performed and revealed to the prophets, but not for other people.
There are many of these, like the deeds of Rabbah bar bar Chana, things which are shocking which Gd showed His pious people who believe in Him wholeheartedly.
3) In some of the midrashim the sages intend to analyze Scripture with any means possible, relying on Tehillim 62:12, "Gd said one thing; I heard two." And so Yirmiyah 23:29, "For My words are as fire; this is the word of Gd. And they are like a hammer, splitting stone." They learned from this that one sentence may lead to many meanings, as explained in Sanhedrin 31a.
Do not be shocked by this; you often see that even a normal person speaks a complex message with two facets, and certainly words of wisdom spoken with Divine inspiration. Along these lines the sages analyze a passage in any way they can analyze it, saying (Shabbat 63a), "The passage does not depart from its simple meaning," which is the essence, and regarding all of the midrashim which are drawn from it, some of them are of the essence and close to the literal read and some of them have a small hint [in the text].
You see what one of the sages taught in Taanit 5b, "Yaakov our ancestor did not die." One sage replied to him, "Did the eulogizers eulogize him and embalmers embalm him and buriers bury him for nothing?" And he responded, "I am analyzing the passage." Meaning: I know he died, but I intend to analyze the passage in any way it can be analyzed, and if the midrash cannot be as it sounds, the passage still offers a hint that one could say "he did not die" as Berachot 18a says, "The righteous live even in their death," for their names and memory and deeds live eternally.
A similar case is seen in Shabbat 30b, in which the exegete taught, "Israel will produce cakes and fine clothing," as it is written, "There will be pisat bar in the land." [See Rashi there, for the connection between pisat and cakes and clothing.] A student mocked him, noting that Kohelet 1:9 says there is nothing new under the sun! To which he replied, "Come and I will show you an example of these items in this world." He went out and showed the student mushrooms. The sage was informing him that the midrash could be explained in a manner which was close to it; the original verse was teaching that the Creator would provide great goodness in the world.
Similar statements occur in other midrashim
They said in Yerushalmi Nazir 7:2, "Are the midrashot amanah? Learn them and receive reward." It is explained that the sages did not state the midrashim as matters of faith [emunah] and as the essence, but to increase the meanings of the text and analyze all of its facets, such that they might include a hint. Links to text and hints are among the paths of Torah study, regarding which it is said, "Learn them and receive reward."
Regarding one who mocks their words it is said (Divrei haYamim II 36:16), "And they mocked the messengers of Gd… and made light of His prophets." In various places we find that they were punished for mocking the words of the sages. Learn from the student who mocked the words of the sage who was analyzing Yeshayah 54:12, "And I will make your windows of gems," and they showed him from heaven, for the honour of that sage, that the words of the sage were accurate and one should not mock them, and the student was punished. (Sanhedrin 100a)